iota枚举

点击查看代码
package main

import "fmt"

func main() {
	/*
		iota:
			是一个常量的自动生成器,自动加1
		 	给常量赋值使用的
			遇到了const就会重置为0
			同一个const中的所有iota都会自动加1
			同一个const中,如果一次定义多个变量(同一行),都是0
	*/
	const (
		a = iota // 0
		b = iota // 1
		c = iota // 2
		a1
		a2
		a3
		a4 = 4
		a5
		a6
		a7 = iota
		a8
		a9
		a10
		a11
	)
	// iota 遇到了const就会重置为0
	const d = iota

	const (
		// 全是0
		e, f, g, h = iota, iota, iota, iota
	)
	fmt.Println(a, b, c)
	fmt.Println(d)
	fmt.Println(e, f, g, h)
	fmt.Println(a1, a2, a3, a4, a5)
	fmt.Println(a6, a7, a8, a9, a10, a11)
}

输出:

点击查看代码
0 1 2
0              
0 0 0 0        
3 4 5 4 4      
4 9 10 11 12 13
posted @ 2022-02-28 11:08  ty1539  阅读(22)  评论(0编辑  收藏  举报