Overview
Go tools are the great features that exist alongside the language as part of the go
command. Almost Go tools come together with Go installation, but there are some you might install depending on your needs.
Installation
The easiest way to install a Go tool is to run go get -u golang.org/x/tools/...
. Another way to do it is to git clone
the repository to $GOPATH/src/golang.org/x/tools
.
There are some helpful flags:
-u
forces the tool to sync with the latest version of the repo.
-d
if you just want to clone a repo to your GOPATH and skip the building and installation phase.
Tools
go vet
The vet
command will check your code for common errors. Let’s look at the types of errors vet
can catch:
- Bad parameters in Printf-style function calls
- Method signature errors for common method definitions
- Bad struct tags
- Unkeyed composite literals
|
|
|
|
go list
It lists the packages named by the import paths, one per line.
go env
It prints Go environment information.
|
|
If there are specific values that you’re interested in, you can pass them as arguments to go env.
|
|
go fmt
Gofmt is a tool that automatically formats Go source code.
To format your code, you can use the gofmt tool directly:
|
|
Or you can use the “go fmt” command:
|
|
It also supports rewrite rules that you can use to help refactor your code.
|
|
|
|