tanish-kr's learning log

Learning output log

Go

ステートメント

ステートメントは実行を制御する

Statement =
    Declaration | LabeledStmt | SimpleStmt |
    GoStmt | ReturnStmt | BreakStmt | ContinueStmt | GotoStmt |
    FallthroughStmt | Block | IfStmt | SwitchStmt | SelectStmt | ForStmt |
    DeferStmt .

SimpleStmt = EmptyStmt | ExpressionStmt | SendStmt | IncDecStmt | Assignment | ShortVarDecl .

EmptyStmt (空ステートメント)

空のステートメント。何もしない

ExpressionStmt (式ステートメント)

関数呼び出し、メソッド呼び出し、受信操作のコンテキスト内に記述できる

SendStmt (送信ステートメント)

チャネル送信のステートメント。チャネル型となる式でなければならない。値はチャネルの要素型に対し、代入可能な値でなければならない。

IncDecStmt (インクリメント/デクリメントステートメント)

  • インクリメント : n++
  • デクリメント : n–

Assignment (代入)

左辺のオペランドはアドレス指定可能か、マップのインデックス式、ブランク識別子のいずれかでなければならない。

ShortVarDecl (省略形式による変数の宣言)

変数の宣言の省略形式で、初期化式は伴うが、型の指定は省略可能。

ForStmt

ブロックの繰り返しを行う。式を() で括る必要はない

for a < b {
  a *= 2
}

for i := 0; i < 10; i++ {
  f(i)
}

配列やMap等の繰り返し

  • rangeを使用する
arr := [...]string{"a", "b", "c"}
for idx, val := range arr {
  fmt.Println(fmt.Sprintf("index = %d; value = %s", idx, val))
}
ages := map[string]string{"name": "Tanaka", "sex": "man", "age": "32"}
for idx, val := range ages {
  fmt.Println(fmt.Sprintf("index = %s; value = %s", idx, val))
}

If

条件分岐式

x := 102
if x > 100 {
  fmt.Println("over")
}

Switch

一般的なswitch文と同等

x := 102
switch x % 2 {
case 0:
  fmt.Println("偶数")
case 1:
  fmt.Println("奇数")
}

参考