GO learning - 001 Variable


GO learning - 001 Variable

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package main

import "fmt"

/*
四种变量的声明方式
*/

func main() {
//方法一:声明一个变量
var a int
fmt.Println("a = ", a)
fmt.Printf("type of a = %T\n", a)

//方法二:声明一个变量,初始化一个值
var b int = 100
fmt.Println("b = ", b)
fmt.Printf("type of b = %T\n", b)

//方法三:初始化的时候,省去数据类型,通过值自动匹配类型
var c = 100
fmt.Println("c = ", c)
fmt.Printf("type of c = %T\n", c)

//方法四:(常用的方法)省去var关键字,直接自动匹配
e := 100
fmt.Println("e =", e)
fmt.Printf("type of e = %T\n", e)

f, g := 2, 3
fmt.Println("f = ", f, " g = ", g)
}
1
2
3
4
5
6
7
8
9
a =  0
type of a = int
b = 100
type of b = int
c = 100
type of c = int
e = 100
type of e = int
f = 2 g = 3

Author: Liang Junyi
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source Liang Junyi !
  TOC