go 常量




// 参考:https://studygolang.com/articles/15905?fr=sidebar
// iota迭代定义常量
//const配合iota关键字使用,可以定义一组由0开始+1迭代的常量 演示语法:

const (
	gender_secret = iota
	gender_male // = 1
	gender_female // = 2
)
//此时,三个常量值,分别是,0, 1, 2 iota的规则是:若iota出现在const()中,那么const()定义的第一行的iota就是0,第二行就是0+1=1,不论iota是否被常量使用。演示如下:

const (
	c1 = 42 // iota = 0,虽然未使用iota,但后边(const()中)有使用,此时iota被初始化为0,下面每行累加1
	c2 = iota      // iota = 1,iota继续累加,使用了iota
	c3 = 1024      // iota = 2,同样未使用,但iota的值继续累加。c3 被定义为1024
	c4             // iota = 3,c4延续上面的定义c4=1024,iota继续累加
	c5      // iota = 4,iota继续累加,使用了iota c5=1024延续上面c4
	c6 = iota      // iota = 5,iota继续累加,使用了iota
)
//此时结果为:42, 1, 1024, 1024, 1024, 5


原文地址:https://www.cnblogs.com/lajiao/p/11540047.html