常量
定义
- 常量使用const关键字进行定义
- 常量的值在编译时确定,所以运行时无法改变
- 常量表达式中的函数必须是内置函数(因为自己写的函数的返回值是在运行时确定的)
示例
const ( text, size = "text", len(text))
常量组中如果不指定默认值则使用上行表达式的值
const ( a = 1 //常量必须有值,所以a必须指定值 b c)fmt.Println(a, b, c) //1 1 1
iota
- iota是常量组中的计数器,从0开始,常量组中每定义一个常量时iota的值会自动加1
- 每定义一个新的常量组时,iota都会清零
const ( d = "D" e f = iota g)const ( k = iota)fmt.Println(d, e, f, g) //D D 2 3fmt.Println(k) //0