Golang之路---02 基础语法——常量 (包括特殊常量iota)

常量
//显式类型定义
    const a string ="test"
//隐式类型定义
	const b = 20
//多个常量定义	
	const(
       c = "test2"
	   d = 2.3
	   e = 27

	)
iota

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

const (
   first = iota
   second = iota
   third = iota
)

const (
	first_1 = iota
	second_1 
	third_1
 )

 const (
	first_2 = iota+1
	second_2 
	third_2
 )


func main() {
   fmt.Println(first, second, third)
   fmt.Println(first_1, second_1, third_1)
   fmt.Println(first_2, second_2, third_2)
}

输出结果:
在这里插入图片描述
注:
iota是const语句块中的行索引,而不是变量索引,其计数只与const语句块中的行数相关

const (
    d1 = iota //0
    d2 = 20  //20
    d3 = iota //2
)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值