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)
输出结果: