Wang-Kai/cherish-today

go vet 使用

Opened this issue · 0 comments

go vet 工具用来对 go 代码做检查,报告可疑的问题代码。Vet 工具仅作为启发式问题报告,比如使用 Printf 使用的 format 和实际参数类型不匹配。它可以发现编译器发现不到的问题。

如何使用?

检查当前目录下的文件

go vet

检查指定路径下的所有文件

go vet my/project/...

go vet 命令常用的 flag 有两个:

  • -c=N 目标代码的上下文行数
  • -json 将结果以 JSON 格式输出

使用场景

go vet 报告出问题的时候,退出码为非零,没有检查到问题时,退出码为 0,这个特性可以添加到代码的 CI 环节。go vet 可以检查到的问题:

asmdecl      report mismatches between assembly files and Go declarations
assign       check for useless assignments
atomic       check for common mistakes using the sync/atomic package
bools        check for common mistakes involving boolean operators
buildtag     check that +build tags are well-formed and correctly located
cgocall      detect some violations of the cgo pointer passing rules
composites   check for unkeyed composite literals
copylocks    check for locks erroneously passed by value
httpresponse check for mistakes using HTTP responses
loopclosure  check references to loop variables from within nested functions
lostcancel   check cancel func returned by context.WithCancel is called
nilfunc      check for useless comparisons between functions and nil
printf       check consistency of Printf format strings and arguments
shift        check for shifts that equal or exceed the width of the integer
stdmethods   check signature of methods of well-known interfaces
structtag    check that struct field tags conform to reflect.StructTag.Get
tests        check for common mistaken usages of tests and examples
unmarshal    report passing non-pointer or non-interface values to unmarshal
unreachable  check for unreachable code
unsafeptr    check for invalid conversions of uintptr to unsafe.Pointer
unusedresult check for unused results of calls to some functions

默认的,所有的检查项都会被执行。

参考文章