go语言iota枚举类型
iota常与const常量搭配,在constraint中,默认开始值为0,每增加一行加1,而且每遇到一个const关键字,iota就会重置为0。
注意:在同一行声明的iota,均为0,理由见下面实例中的注释。
iota枚举实例
package main
import (
"fmt"
)
func main() {
const (
a = iota //a=0
b = iota //b=1
c = "中间断开并不影响iota自动加1的属性,除非遇到另一个const关键词"
d = iota //c=2
)
const e = iota //e=0
const f = iota //f=0
const g, h, i = iota, iota, iota //g、h、i在同一行,相当于const g、const h和const i,所以三者皆为0
fmt.Println(a, b, c, d, e, f)
fmt.Println(g, h, i)
}
——————————————————————————————————
代码输出:
0 1 中间断开并不影响iota自动加1的属性,除非遇到另一个const关键词 3 0 0
0 0 0