Go中iota除了在const内部做行索引计数和定义数量级外,还有什么实用性嘛?

请问一下大佬们,Go语言中的iota除了在const内部做行索引计数和定义数量级外,还有什么实用性嘛?

package main

type ByteSize float64
//定义数量级
const (
	_ = iota //用下划线忽略第一个变量,此时iota=0
	KB ByteSize = 1 << (10*iota)  //1 << (10*1),此时iota=1
	MB //1 << (10*2),此时iota=2
	GB //1 << (10*3),此时iota=3
	TB //1 << (10*4),此时iota=4
	PB //1 << (10*5),此时iota=5
	EB //1 << (10*6),此时iota=6
	ZB //1 << (10*7),此时iota=7
	YB //1 << (10*8),此时iota=8
)

func main() {
	println("KB =", KB)
	println("MB =", MB)
	println("GB =", GB)
	println("TB =", TB)
	println("PB =", PB)
	println("EB =", EB)
	println("ZB =", ZB)
	println("YB =", YB)
}
golang
73 views
Comments
登录后评论
Sign In
·

留名

·
const (
	read   = 1 << iota // 00000001 = 1
	write              // 00000010 = 2
	remove             // 00000100 = 4

	// admin will have all of the permissions
	admin = read | write | remove
)

基本上就是枚举的用途,设计的时候就是这样用的,没其他了