package main
import "fmt"
func main() {
// -------------------------
// 一般用于给常量赋值使用
const (
// 会自动累加:0
a = iota
// 会自动累加:1
b = iota
// 会自动累加:2
c = iota
)
// 遇到const会重置为0
const d = iota
fmt.Println(a, b, c, d)
// -------------------------
const (
// 会自动累加:0
// 可以只写一个iota
a1 = iota
// 会自动累加:1
b1
// 会自动累加:2
c1
)
fmt.Println(a1, b1, c1)
// -------------------------
const (
// 会自动累加:0
// 可以只写一个iota
a2 = iota
// 如果在同一行,iota赋予的值也是一样的
b2, c2, d2 = iota, iota, iota
e2 = iota
)
fmt.Println(a2, b2, c2, d2, e2)
}
Go中Iota的使用
最新推荐文章于 2024-01-24 16:24:28 发布