3.2. iota枚举
iota枚举
简单的说法
iota枚举格式如果写在一行中值相等 如果换行值在上一行加一。
复杂的说法
常量声明可以使用iota常量生成器初始化,它用于生成一组以相似规则初始化的常量,但是不用每行都写一遍初始化表达式。
注意:在一个const声明语句中,在第一个声明的常量所在的行,iota将会被置为0,然后在每一个有常量声明的行加一。
1. iota 加一演示
const (
a = iota
b, c = iota, iota
)
2. iota 只需要对第一个进行iota赋值 后面会依次增长
const (
a = iota
b
c
d
)
3. 在定义iota枚举时可以自定义赋值
const (
a = iota
b = 10
c = 20
d
e
f = iota
g
)
fmt.Println(a)
fmt.Println(b)
fmt.Println(c)
fmt.Println(d)
fmt.Println(e)
fmt.Println(f)
fmt.Println(g)
输出结果: