注意:Go 1.18版本iota的bug

3,634 阅读2分钟

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 == 0
const y = iota  // y == 0
const (
    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 main

import "fmt"

const C1 = iota
const C2 = iota

func 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 = iotaconst 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 main

import "fmt"

const (
    C1 = iota
)
const (
    C2 = iota
)

func test1() {
    fmt.Println("C1=", C1, " C2=", C2)
}

func main() {
    test1()
}

上面程序的执行结果是:

C1= 0  C2= 0

而且,对于局部常量声明也是不会有这个bug。

package main

import "fmt"

func test1() {
    const C1 = iota
    const C2 = iota
    fmt.Println("C1=", C1, " C2=", C2)
}

func main() {
    test1()
}

上面程序的执行结果是:

C1= 0  C2= 0

推荐阅读

开源地址

文章和示例代码开源在GitHub: Go语言初级、中级和高级教程

公众号:coding进阶。

个人网站:Jincheng’s Blog

References