go-vet
Analyze Go code for common mistakes
TLDR
Check the Go package in the current directory
Check the Go package in the specified path
List available checks that can be run with go vet
View details and flags for a particular check
Display offending lines plus n lines of surrounding context
Output analysis and errors in JSON format
SYNOPSIS
go vet [vet-flags] [packages]
Where:
vet-flags: Flags passed directly to the underlying vet analysis tool.
packages: One or more package paths (e.g., ./... for all subpackages, . for the current package, or specific package import paths like golang.org/x/tools/go/vet).
PARAMETERS
-all
Run all available analysis checkers.
-asmdecl
Check for inconsistencies in assembly declarations.
-atomic
Check for common mistakes when using the sync/atomic package.
-bools
Check for common mistakes involving boolean expressions.
-buildtags
Check for errors in //go:build and // +build directives.
-composites
Check for unkeyed composite literals.
-fieldalignment
Check for structs whose fields are not optimally aligned.
-nilness
Check for nil pointer dereferences and other nil-related issues.
-printf
Check for incorrect printf format strings (default behavior).
-shadow
Check for shadowed variables.
-stdmethods
Check for incorrect signatures of standard library methods.
-structtags
Check for malformed or uncanonical struct tags.
-test
Apply checks specific to Go test files.
-unreachable
Check for unreachable code.
-unusedresult
Check for unused results of function calls that return an error or other important value.
-tags=
A comma-separated list of build tags to consider satisfied during analysis.
-v
Enable verbose output from the vet tool.
DESCRIPTION
The go vet command is a static analysis tool that examines Go source code and reports suspicious constructs, potential errors, and non-idiomatic uses that the compiler might not catch. It is part of the standard Go toolchain and helps developers identify common mistakes early in the development cycle.
While it does not replace testing, go vet serves as a valuable first line of defense against bugs by flagging issues like incorrect printf format strings, unreachable code, unkeyed composite literals, improper use of synchronization primitives, and more. It acts as a "vetter" for code quality, focusing on correctness rather than stylistic preferences (which are handled by tools like go fmt). Running go vet regularly on a codebase contributes significantly to maintaining high code quality and reducing the likelihood of runtime errors. Many continuous integration (CI) pipelines include go vet as a mandatory step.
CAVEATS
While powerful, go vet is a static analysis tool and cannot detect all possible runtime errors or complex logical bugs. It may occasionally produce false positives, requiring developers to evaluate the reported issue in context. Its focus is on common suspicious patterns rather than strict coding style enforcement (which is often handled by dedicated linters). The analysis can be resource-intensive on very large codebases, especially when using the -all flag, or when dealing with complex dependency graphs.
INTEGRATION WITH <I>GO TEST</I>
The go test command can automatically run go vet before running tests. By default, it runs go vet if any packages are specified as arguments. To explicitly control this behavior, use the -vet flag:
go test -vet=off (to disable vet)
go test -vet=all (to enable vet for all specified packages)
go test -vet=printf (to enable only specific vet checks)
RUNNING ON MULTIPLE PACKAGES
To run go vet on all packages within the current module or directory tree, use the special ./... argument:
go vet ./...
This is a common practice in CI/CD pipelines and local development to ensure comprehensive analysis across the entire project.
HISTORY
go vet originated as a standalone tool, vet, existing even before the Go language's open-source release. It was later integrated into the standard go command as go tool vet, and then made directly accessible via go vet. Its consistent presence in the Go toolchain underscores the language's strong emphasis on tooling for code quality and correctness. Over time, new checkers have been added, and existing ones improved, making it an increasingly robust and essential part of the Go development workflow.