Golang iota

golang iota就是用于创建递增常量的概念

基本使用就是像下面这样

const (
	Red int = iota
	Orange
	Yellow
)

从0开始,Red为0,Orange为1,Yellow为2

如果我们想跳过某些值,可以使用_

const (
	_ int =iota
  Red
)
// 此时Red为1

当然如果不用_也是会跳过的

const (
	skip = "skip"
  Red  = iota
)
// 此时Red为1

如果要递增操作,比如bitmask,那么可以采用下列方式

const (
	read = 1 << iota // 00000001
  write // 00000010
)

在一些愚蠢的面试题可能会出现以下操作

const (
	tomato, apple int = iota + 1, iota + 2
	orange, chevy
	ford, _
)

// tomato =  1, apple = 2
// orange =  2, chevy = 3
// ford   =  3
  1. https://www.gopherguides.com/articles/how-to-use-iota-in-golang
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值