Go vet command is a handy tool that can help you write better and cleaner code. It helps you find errors in your application and reports suspicious constructs such as abnormal or useless code, as well as detect common mistakes using different packages.
Vet command (or should we say vet commands?) is a collection of different analysis tool commands and sub-analyzers. It is worth mentioning that vet uses heuristics which can’t guarantee that 100% of the reports will be genuine issues. However, it can detect errors that compliers might omit.
Using the vet command
You can invoke vet with the go command. The following command will vet the current directory package:
go vet
If you wish to vet a package in another directory you need to provide a path:
go vet my_machine/project/...
If you want to vet specific packages you can use:
go help packages
Listing all available checks with tool vet help
You can list all available checks by running the:
go tool vet help
Now, let’s briefly go over what you can exactly get from the go tool vet help commands.
asmdecl
asmdecl reports mismatches between the Go declarations and assembly files.
assign
This is a check for useless assignments.
atomic
This is for common mistakes using the sync/atomic package and will check any irregularities in the usage of the atomic function.
bools
Detects common mistakes that involve boolean operators.
buildtag
Checks the form and location of the +build tags. The +build flags supported by go vet are related to the resolution and execution of the control package.
cgocall
Checks for violations of the cgo pointer passing rules.
composites
Looks for any composite literals using unkeyed fields.
copylocks
Detects copied locks passed by value as an error. The locks should never be copied, as the copy of the lock will copy the internal state of the “original” making it the same as the “original”.
httpresponse
Checks for any mistakes in HTTP responses usage
loopclosure
Checks for any references to loop variables from within nested functions.
lostcancel
Detects cancel func that is returned by context.WithCancel. Creating a cancellable context will return a new context with a function that enables you to cancel that particular context. You can use that function to cancel each and every operation linked to that context, however, it has to be called in order. Otherwise, it will leak context.
nilfunc
Searches for useless comparisons between nil and functions.
printf
Checks for consistency in Printf format. Constructs such as Printf calls with arguments that don’t align with the format string will be flagged.
shift
This go vet analysis tool will try to find shifts that either exceed or equal the integer width.
stdmethods
Checks for methods that you have implemented from the standard library interfaces and their compatibility.
structtag
Detects fields that don’t conform to the convention defined in the reflect package.
tests
This analyzer catches likely mistakes made while using either a test or an example.
unmarshal
It returns all instances of passing non-interface or non-pointer values to unmarshal.
unreachable
Simply checks for any code that is unreachable.
unsafeptr
Detects any invalid conversion of uintptr to unsafe.Pointer.
unusedresults
Detects unused results of calls to functions.
Conclusions on the go vet analysis tool
Go vet is a very useful collection of tools that can help you find different issues with your source code. By design, all checks are performed, unless any flags are set to true. In that case, only those tests will be run. Analogically, setting a flag to false will disable that particular test.
Please mind that not all returned errors are genuine problems. Moreover, if you wish to look for other subtle issues in your go programs, one that no specific checker detects, there is an option to add additional checks and custom analyzers.