LoginSignup
0
0

More than 1 year has passed since last update.

【自己学習】Go言語を学ぶ(3)

Posted at

公式チュートリアルから基本を学ぶ
https://go-tour-jp.appspot.com/list

Variables(変数)

  • varで変数宣言を行える。

サンプルコード

  • c, python, javaはbool型
  • i はint型
package main

import "fmt"

var c, python, java bool

func main() {
  var i int
  fmt.Println(i, c, python, java)
}

結果:0 false false false

Variables with initializers(変数の初期化)

  • var 宣言では、変数毎に初期値を指定できます。

サンプルコード

  • i jをint型で i = 1 j = 2を指定
package main

import "fmt"

var i, j int = 1, 2

func main() {
  var c, python, java = true, false, "no!"
  fmt.Println(i, j, c, python, java)
}

結果:1 2 true false no!

Short variable declarations(短い変数宣言)

  • var 宣言の代わりに、短い := の代入文を使い、型宣言ができる。

サンプルコード

  • k := 3で代入
package main

import "fmt"

func main() {
  var i, j int = 1, 2
  k := 3
  c, python, java := true, false, "no!"

  fmt.Println(i, j, k, c, python, java)
}

結果:1 2 3 true false no!

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