轻松一刻:Go 1.18修复了一个经典bug

2,293 阅读2分钟

前言

大家在写Go的时候,初期应该都会遇到过下面的编译报错:

declared but not used

比如下面的代码示例:

// example2.go
package main

func main() {
	a := 10
	a = 1
}

执行 go run example2.go,会报如下编译错误:

./example2.go:5:2: a declared but not used

原因

上面的报错,实际上是因为对于标准Go编译器,局部变量必须至少有一次被作为右值(r-value: right-hand-side-value)使用过。

上面的例子里局部变量a都是左值,并没有作为右值被使用过,因此编译报错。

我们来看一个新的例子,大家觉得下面的代码,执行go run example2.go的结果是什么?

// example2.go
package main

func main() {
	a := 10
	func() {
		a = 1
	}()
}

结论

按理来说,上面的示例里头,局部变量a在闭包里没有被作为右值使用过,还是应该会编译报错”declared but not used“。

但是大家知道么,Go标准编译器对于”declared but not used“这样的检查,也会有bug。

上面的代码如果是Go1.18之前的版本,执行的话,结果是没有任何报错。

从Go1.18版本开始,解决了局部变量在闭包里没被使用但是编译器不报错的bug,Go1.18开始编译会报错”declared but not used“。

官方说明如下:

The Go 1.18 compiler now correctly reports "declared but not used" errors
for variables that are set inside a function literal but are never used. Before Go 1.18,
the compiler did not report an error in such cases. This fixes long-outstanding compiler
issue #8560. As a result of this change,
(possibly incorrect) programs may not compile anymore. The necessary fix is
straightforward: fix the program if it was in fact incorrect, or use the offending
variable, for instance by assigning it to the blank identifier _.
Since go vet always pointed out this error, the number of affected
programs is likely very small.

关于这个bug修改的讨论,感兴趣的可以参考cmd/compile: consistently report "declared but not used" errors· Issue #49214

开源地址

知乎上分享的所有Go知识和代码都开源在:github.com/jincheng9/g…

也欢迎大家关注微信公众号:coding进阶,学习更多Go、微服务和云原生架构相关知识。