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
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论