GO语言:const常量
常量声明
单行常量声明,声明时必须指定值,且无法修改
const pi = 3.14
多行常量声明,若省略赋值,则默认与上一行值相同
const (
a = 100
b = 200
c // c = 200
d // d = 200
)
iota 常量计数器
①iota只能在常量表达式中使用
②遇到const关键字重置为0
const (
aa = iota // 0
bb // bb = iota 1
cc // cc = iota 2
dd // dd = iota 3
)
const (
n1 = iota // 0
n2 // n2 = iota 1
_ // _ = iota 2
n4 // n4 = iota = 3
)
注意:_匿名变量接收iota的值,iota照常自增,所以n4 == 3
// iota 只要const中每新增一行变量声明,就加一
const (
x1 = iota // 0
x2 = 100 // x2 = 100 iota = 1
x3 = iota // 2
x4 // x4 = iota 3
)
const x5 = iota // 0
iota遇到常量声明就会自增1,遇到const关键字就重置为0
const (
y1, y2 = iota + 1, iota + 2 // iota = 0 y1 = 1 y2 = 2
y3, y4 // iota = 1, y3 = iota + 1 = 2, y4 = iota + 2 = 3
y5, y6 // iota = 2, y5 = iota + 1 = 3, y6 = iota + 2 = 4
)