const与iota知识点注意事项
前面的const声明部分
const (
BEIJING = 10 * iota
SHANGHAI
SHENZHEN
)
const (
a, b = iota + 1, iota + 2
c, d
e, f
g, h = iota * 2, iota * 3
i, k
)
main函数体部分
func main() {
const length int = 0
fmt.Println("length = ", length)
fmt.Println("BEIJING = ", BEIJING)
fmt.Println("SHANGHAI = ", SHANGHAI)
fmt.Println("SHENZHEN = ", SHENZHEN)
fmt.Println("a = ", a, "b = ", b)
fmt.Println("c = ", c, "d = ", d)
fmt.Println("e = ", e, "f = ", f)
fmt.Println("g = ", g, "h = ", h)
fmt.Println("i = ", i, "k = ", k)
}

总结
- 在const()添加一个关键字iota,每行的iota都会累加1,第一行的iota的默认值是0
- iota 只能配合const()一起使用,iota只有在const进行累加效果