iota枚举

iota枚举

常量声明可以使用iota常量生成器初始化,它用于生成一组以相似规则初始化的常量,

但是不用每行都写一遍初始化表达式。

package main

import "fmt"

func main() {
	const (
		x = iota // x == 0
		y = iota // y == 1
		z = iota // z == 2
		w        // 这里隐式地说w = iota,因此w == 3。其实上面y和z可同样不用"= iota"
	)

	const v = iota // 每遇到一个const关键字,iota就会重置,此时v == 0

	const (
		h, i, j = iota, iota, iota //h=0,i=0,j=0 iota在同一行值相同
	)

	const (
		a       = iota             //a=0
		b       = "B"              //b=B, string类型
		c       = iota             //c=2
		d, e, f = iota, iota, iota //d=3,e=3,f=3
		g       = iota             //g = 4
	)

	const (
		x1 = iota * 10 // x1 == 0
		y1 = iota * 10 // y1 == 10
		z1 = iota * 10 // z1 == 20
	)
}

以上。

朱子家训说:宜未雨而筹谋,勿临渴而掘井。 任何事情要到了跟前才想解决办法,那我们岂不很被动!
原文地址:https://www.cnblogs.com/jianyingjie/p/11359911.html