iota是golang语言的常量计数器,只能在常量的表达式中使用。
iota在const关键字出现时将被重置为0,const中每新增一行常量声明将使iota计数一次。
使用方法:
1. iota只能在常量的表达式中使用
fmt.Println(iota)
编译错误: undefined: iota
2. 每次 const 出现时,都会让 iota 初始化为0
const a = iota // a=0
const (
b = iota //b=0
c //c=1
)
3. 自定义类型,自增长常量经常包含一个自定义枚举类型,允许你依靠编译器完成自增设置
type status int
const (
unknow status = iota // 0
queued // 1
queued // 2
queued // 3
)
4. 可跳过的值
type status int
const (
unknow status = iota // 0
queued // 1
queued // 2
_
_
queued // 5
)
5. 定义在一行的情况
const (
a, b = iota + 1, iota + 2
c, d
e, f
)
a //1
b //2
c //2
d //3
e //3
f //4