变量
使用var关键字
- var a,b,c bool
- var s1,s2 string = "hello","world"
- 可放在函数内,或直接放在包内
- 使用var()集中定义变量
- 编译器可以自动决定类型
var a,b,i,s1,s2 = true,false,3,"hello","world"
- 可以使用:=定义变量,这样子短一些方便(推荐),只能在函数内使用,并且再次赋值的时候只能用=
a,b,i,s1,s2:= true,false,3,"hello","world"
内建变量类型
<details>
<summary>展开查看</summary>
bool string
(u)int,(u)int8,(u)int16,(u)int32,(u)int64,uintptr
byte,rune
float32,float64,complex64,complex128
</details>
常量
- const filename = "abc.txt"
- const 数值可作为各种类型使用
- const a,b = 3,4
- var c int = int(math.Sqrt(aa + bb))此时ab自动会当float来用
使用常量定义枚举类型
const (
iOS = iota
_
vue
golang
)
const (
b = 1 << (10 * iota)
kb
mb
gb
)
网友评论