Go iota枚举(常量的集合)

demo.go(iota枚举):

package main

import "fmt"

func main(){
	// iota枚举 (常量的集合)
	const(
		a = iota  // iota 默认从0开始,每次换行就会加1
		b = iota
		c,d = iota,iota  // 同一行的iota的值相同
	)
	fmt.Println(a)  // 0
	fmt.Println(b)  // 1
	fmt.Println(c)  // 2
	fmt.Println(d)  // 2

	// iota枚举 (第二种方式)
	const(
		l = iota  // 可以只对第一个进行赋值,后面的依次加1
		m
		n
		o = 100  // 也可以手动赋值
		p
		q
		r = iota
		s
	)
	fmt.Println(l)  // 0
	fmt.Println(m)  // 1
	fmt.Println(n)  // 2
	fmt.Println(o)  // 100
	fmt.Println(p)  // 100
	fmt.Println(q)  // 100
	fmt.Println(r)  // 6
	fmt.Println(s)  // 7

}

猜你喜欢

转载自blog.csdn.net/houyanhua1/article/details/88603929