常量
//显式类型定义const a string ="test"
//隐式类型定义const b = 20
//多个常量定义 const(c = "test2"d = 2.3e = 27)
iota
iota是Golang语言的常量计数器,只能在常量表达式中使用
iota在const关键字出现时将被重置为0,const中每新增一行常量声明将使iota计数一次
const (first = iotasecond = iotathird = iota
)const (first_1 = iotasecond_1 third_1)const (first_2 = iota+1second_2 third_2)func main() {fmt.Println(first, second, third)fmt.Println(first_1, second_1, third_1)fmt.Println(first_2, second_2, third_2)
}
输出结果:
注:
iota是const语句块中的行索引,而不是变量索引,其计数只与const语句块中的行数相关
const (d1 = iota //0d2 = 20 //20d3 = iota //2
)