0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

入門GOプログラミング Lesson2 偉そうな計算機

Last updated at Posted at 2023-03-04

整形付き出力

標準出力
Print :
Println :
Printf : Print formatの略(だと思われる)
以下の使い方が可能

    fmt.Print("Hello World")
	fmt.Println("kgです")// 自動改行付き
	fmt.Printf("私の体重は%v表面で%vkgです", "Earth", 70)//フォーマット指定子付き

テキストのアライメント調整

	fmt.Printf("%-15v $%4v\n", "spaceX", 94)// 右に15文字分パディングを設ける
	fmt.Printf("%-15v $%4v\n", "Virgin Galactic", 100)//左に4文字分パディングを設ける

定数と変数

const lightSpeed = 300000 // 定数定義
var distance = 1000000000 // 変数定義

ショートカット定義も可能

var (
    distance = 50000000
    speed = 100800
)

var distance, speed = 50000000, 100800

代入演算子・インクリメント

代入演算子
var weight = 70
weight *= 0.39
インクリメント
var weight = 70
weight + weight + 1
weight += 1// 同上
weight++// 同上

デクリメントはweight--

乱数

インクリメント
import (
    "math/rand"
)

■練習問題

malacandra.go
package main

import (
	"fmt"
)

func main() {
	const distanceToMalacandra = 56000000
	var spendDays = 28
	var speed = distanceToMalacandra / spendDays
	fmt.Printf("%vkm/sec", speed)
}

メモ

変数の宣言はできない
var distancemissing variable type or initialization

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?