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:
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 reports mismatches between the Go declarations and assembly files.
This is a check for useless assignments.
This is for common mistakes using the sync/atomic package and will check any irregularities in the usage of the atomic function.
Detects common mistakes that involve boolean operators.
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.
Checks for violations of the cgo pointer passing rules.
Looks for any composite literals using unkeyed fields.
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”.
Checks for any mistakes in HTTP responses usage
Checks for any references to loop variables from within nested functions.
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.
Searches for useless comparisons between nil and functions.
Checks for consistency in Printf format. Constructs such as Printf calls with arguments that don’t align with the format string will be flagged.
This go vet analysis tool will try to find shifts that either exceed or equal the integer width.
Checks for methods that you have implemented from the standard library interfaces and their compatibility.
Detects fields that don’t conform to the convention defined in the reflect package.
This analyzer catches likely mistakes made while using either a test or an example.
It returns all instances of passing non-interface or non-pointer values to unmarshal.
Simply checks for any code that is unreachable.
Detects any invalid conversion of uintptr to unsafe.Pointer.
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.