当我们在开发一个大型的Go项目时,往往很难去控制大家都能够写出来高质量的代码,但是我们还是经常会在脑子里面有这些想法:
1. 格式化
2. Imports
3. Error处理
4. 文档注释
5. 导出函数和结构体等
6. unused的代码必须清除
7. 控制代码的复杂度,例如一个函数不能多于50行
8. 重复的代码抽象出来
这里有非常多的东西需要去记住,那么有没有一个工具可以帮助我们呢?
今天就给大家推荐这个工具:gometalinter
https://github.com/alecthomas/gometalinter
这个工具基本上集成了目前市场上所有的检测工具,然后可以并发的帮你静态分析你的代码:
go vet — Reports potential errors that otherwise compile.
go vet — shadow — Reports variables that may have been unintentionally shadowed.
gotype — Syntactic and semantic analysis similar to the Go compiler.
deadcode — Finds unused code.
gocyclo — Computes the cyclomatic complexity of functions.
golint — Google’s (mostly stylistic) linter.
defercheck — Checks for duplicate defer calls.
varcheck — Find unused global variables and constants.
structcheck — Find unused struct fields.
errcheck — Check that error return values are used.
dupl — Reports potentially duplicated code.
这些是目前集成的工具。
安装方式也是很简单:
$ go get github.com/alecthomas/gometalinter
$ gometalinter --install --update
那么怎么使用它呢?
$ gometalinter ./...
stutter.go:13::warning: unused struct field MyStruct.Unused (structcheck)
stutter.go:9::warning: unused global variable unusedGlobal (varcheck)
stutter.go:12:6:warning: exported type MyStruct should have comment or be unexported (golint)
stutter.go:16:6:warning: exported type PublicUndocumented should have comment or be unexported (golint)
stutter.go:22::error: Repeating defer a.Close() inside function duplicateDefer (defercheck)
stutter.go:8:1:warning: unusedGlobal is unused (deadcode)
本文分享自微信公众号 - GoCN(golangchina)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。
来源:oschina
链接:https://my.oschina.net/u/4628563/blog/4793226