Iota
iota
是Go语言的预申明标识符,用于常量的申明。
iota
的值是const
语句块里的行索引,值从0开始,每次递减少1。通过上面的代码示例咱们先回顾下iota
的个性。
const ( c0 = iota // c0 == 0 c1 = iota // c1 == 1 c2 = iota // c2 == 2)const ( a = 1 << iota // a == 1 (iota == 0) b = 1 << iota // b == 2 (iota == 1) c = 3 // c == 3 (iota == 2, unused) d = 1 << iota // d == 8 (iota == 3))const ( u = iota * 42 // u == 0 (untyped integer constant) v float64 = iota * 42 // v == 42.0 (float64 constant) w = iota * 42 // w == 84 (untyped integer constant))const x = iota // x == 0const y = iota // y == 0const ( class1 = 0 class2 // class2 = 0 class3 = iota //iota is 2, so class3 = 2 class4 // class4 = 3 class5 = "abc" class6 // class6 = "abc" class7 = iota // class7 is 6)
Bug
2022年3月15日,Go官网团队正式公布了Go 1.18版本。Go 1.18是Go语言诞生以来变动最大的版本,引入了泛型、Fuzzing、工作区模式等泛滥新性能和性能优化。
天下没有无bug的零碎,Go当然也不例外。Go 1.18引入了一个和iota
相干的bug。
大家看看上面这段程序,思考下输入后果应该是什么?
package mainimport "fmt"const C1 = iotaconst C2 = iotafunc test1() { fmt.Println("C1=", C1, " C2=", C2)}func main() { test1()}
先思考几秒钟。。。
在Go 1.18版本之前,上述程序打印的后果是
C1= 0 C2= 0
在Go 1.18版本,上述程序打印的后果是
C1= 0 C2= 1
很显然,这是一个bug,因为const C1 = iota
和const C2 = iota
是相互独立的const
语句块,因而这2个const
申明里的iota
的值都是0。
Go官网也认领了这个bug,Go语言的次要设计者Robert Griesemer解释了这个bug产生的起因:
No need to bisect. This is due to a completely new type checker, so it won't be useful to pin-point to a single change. I've identified the bug and will have a fix in a little bit.
This is clearly a bad bug; but only manifests itself when using iota outside a grouped constant declaration, twice.
As a temporary work-around, you can change your code to:
// OpOr is a logical or (precedence 0)const (OpOr Op = 0 + iota<<8)// OpAnd is a logical and (precedence 1)const (OpAnd Op = 1 + iota<<8)
(put parentheses around the const declarations).
产生这个bug是因为Go引入了全新的类型查看器导致的。
这个bug只有在全局、未分组的常量申明才会呈现,该bug预计会在Go 1.19版本进行修复。
咱们用括号()
将申明包起来,也就是应用分组的常量申明,就不会有这个bug了。
package mainimport "fmt"const ( C1 = iota)const ( C2 = iota)func test1() { fmt.Println("C1=", C1, " C2=", C2)}func main() { test1()}
下面程序的执行后果是:
C1= 0 C2= 0
而且,对于部分常量申明也是不会有这个bug。
package mainimport "fmt"func test1() { const C1 = iota const C2 = iota fmt.Println("C1=", C1, " C2=", C2)}func main() { test1()}
下面程序的执行后果是:
C1= 0 C2= 0
举荐浏览
泛型
- 泛型:Go泛型入门官网教程
- 泛型:一文读懂Go泛型设计和应用场景
- 泛型:Go 1.18正式版本将从规范库中移除constraints包
- 泛型:什么场景应该应用泛型
Fuzzing
- Fuzzing: Go Fuzzing入门官网教程
- Fuzzing: 一文读懂Go Fuzzing应用和原理
工作区模式
- Go 1.18:工作区模式workspace mode简介
- Go 1.18:工作区模式最佳实际
开源地址
文章和示例代码开源在GitHub: Go语言高级、中级和高级教程。
公众号:coding进阶。关注公众号能够获取最新Go面试题和技术栈。
集体网站:Jincheng’s Blog。
知乎:无忌
References
- https://twitter.com/go100and1...
- https://github.com/golang/go/...
- https://go.dev/ref/spec#Iota