关于go:go-常用代码检查工具

如何主动发现variable shadowing

靠人肉去排查还是容易脱漏的,Go工具链里有一个shadow命令能够帮忙咱们排查代码里潜在的variable shadowing问题。

第一步,装置shadow命令

go install golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow

第二步,应用shadow查看代码里是否有variable shadowing

go vet -vettool=$(which shadow)

比方,我查看后的后果如下:

$ go vet -vettool=$(which shadow)
# example.com/shadow
./main.go:9:6: declaration of "i" shadows declaration at line 8

此外,shadow命令也能够独自应用,不须要联合go vet。shadow前面须要带上package名称或者.go源代码文件名。

$ shadow example.com/shadow
11-variable-shadowing/main.go:9:6: declaration of "i" shadows declaration at line 8
$ shadow main.go
11-variable-shadowing/main.go:9:6: declaration of "i" shadows declaration at line 8

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理