golang 使用 iota

iota是golang语言的常量计数器,只能在常量的表达式中使用。
iota在const关键字出现时将被重置为0,const中每新增一行常量声明将使iota计数一次。

使用方法:
1. iota只能在常量的表达式中使用
fmt.Println(iota)
编译错误: undefined: iota

2. 每次 const 出现时,都会让 iota 初始化为0
const a = iota    // a=0
const (
    b = iota    //b=0
    c            //c=1
)

3. 自定义类型,自增长常量经常包含一个自定义枚举类型,允许你依靠编译器完成自增设置
type status int
const (
    unknow status = iota    // 0
    queued                    // 1
    queued                     // 2
    queued                     // 3
)

4. 可跳过的值
type status int
const (
    unknow status = iota     // 0
    queued                    // 1
    queued                    // 2
    _
    _
    queued                    // 5
)

5. 定义在一行的情况
const (
    a, b = iota + 1, iota + 2
    c, d
    e, f
)

a //1
b //2
c //2
d //3
e //3
f //4

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值