在recent question中,使用了导致数据争用的嵌套循环。 go vet
仅捕获了一些问题。
使用简化版:
for _, o := range ol {
o := o // <- remove this: `go vet` does *NOT* complain
for _, d := range dl {
d := d // <- remove this: `go vet` will complain
go func() {
fmt.Printf("%03d %03d\n", o, d) // potential data-race
wg.Done()
}()
}
}
[go vet
正确地检测到内部竞争条件,但不能正确检测到外部竞争条件。
为什么?太复杂以至于无法跟踪超过2个级别的范围?
[评论in the code说:
此分析器从一个内部检查是否引用了循环变量循环体内的函数文字。它只检查实例函数文字是在defer或go语句中调用的,即循环主体中的最后一条语句,否则我们将需要整个程序分析。
go vet
命令不会抱怨o
,因为go语句不是o
循环中的最后一条语句。