
Usage of Go is increasing within the company. Due to it being a compiler as well as a static type language, one of its key features is fast execution speed. Besides that, it is easy to learn due to its simple setup, easy enough that programs written using it can be run on multiple platforms.
With its increasing popularity, we foresee that more teams will begin to use Go. While preserving the quality of the code, in order to improve on serviceability, let’s use software to check the code… there are a few different types of software to choose from, but this time, we’d like to introduce go vet, software that is standardized for and built into Go.
how to use go vet
go vet is run by using the specification method go tool vet [directory]
.
$ go tool vet path/to/go/project
After executing, areas with problems will be indicated in the output.
$ go tool vet ./alecthomas/gometalinter/
adjcmartix.go:85: suspect or: char != "" || char != " "
main.go:55: arg usage in Fprint call is a function value, not a function call
resolve.go:161: unreachable code
sparse.go:58: _m might be too small for shift of 32
After that, just make the necessary edits to the source code according to the identified points.
Help
go vet’s help can be output using go doc cmd/vet
.
$ go doc cmd/vet
Vet examines Go source code and reports suspicious constructs, such as
Printf calls whose arguments do not align with the format string. Vet uses
heuristics that do not guarantee all reports are genuine problems, but it
can find errors not caught by the compilers.
It can be invoked three ways:
By package, from the go tool:
go vet package/path/name
vets the package whose path is provided.
By files:
go tool vet source/directory/*.go
:
Regarding options
Items for analysis can be designated in the options. By default, -all
is selected, which will conduct a check on all items. For example, to search for unreachable code, use -unreachable
.
$ go tool vet -unreachable ./alecthomas/
alecthomas/gometalinter/_linters/src/golang.org/x/text/unicode/cldr/resolve.go:161: unreachable code
alecthomas/gometalinter/vendor/gopkg.in/yaml.v2/decode.go:123: unreachable code
Besides that, to change pointers to a numerical value, if you use unsafe.Pointer
there is an option to show a warning -unsafeptr
.
$ go tool vet -unsafeptr ./alecthomas/
alecthomas/gometalinter/_linters/src/golang.org/x/tools/go/ssa/interp/external.go:302: possible misuse of unsafe.Pointer
// the code will be as follows:
if pc != 0 {
fn = (*ssa.Function)(unsafe.Pointer(pc)) // indeed unsafe!
}
Although it’s basically alright to check all the items, please try to assign flags depending on requirements, and just execute the type of item/analysis that is required.
go vet is standardized, and will identify troublesome points based on previous experience. In short, the identified item is not always the problem. Nonetheless, it may also identify problems that did not show up in the compiler. By making edits alongside the contents, you should be able to write code that is easy to understand.
Many code analyzing tools for the Go language cause a heavy load on CPU resources. As such, it will take time when run on a local PC, and you might find it frustrating. When that happens, in coordination with Pull Request on GitHub, execute a Go language analytical tool, Sider, a CI service for automating code reviewing, should be of help. Sider is compatible with go vet. A free trial is available, so please try it first。