1. iota 原理
iota 是特殊常量,在编译阶段确定
2. iota 的使用
const (
test1 = iota
test2 = iota + 1
test3
test4
test5 = "aaaaa"
test6
test7
test8 = iota
)
fmt.Println(test1, test2, test3, test4, test5, test6, test7, test8)
// 输出
// 0 2 3 4 aaaaa aaaaa aaaaa 7
-
初始值
iota 从0开始 -
递增规则
在一组const 中,每一个const 常量都会对iota 进行+1,test3 因为没有指定类型和值,所以相当于
test3 = iota + 1,所以值为3,test5 虽然为string 类型,但是依然会对iota 进行 +1 -
iota 重新计数
重新一组const 常量就会对iota 从0重新递增