LinuxCommandLibrary

go-vet

Analyze Go code for common mistakes

TLDR

Check the Go package in the current directory

$ go vet
copy

Check the Go package in the specified path
$ go vet [path/to/file_or_directory]
copy

List available checks that can be run with go vet
$ go tool vet help
copy

View details and flags for a particular check
$ go tool vet help [check_name]
copy

Display offending lines plus n lines of surrounding context
$ go vet -c=[n]
copy

Output analysis and errors in JSON format
$ go vet -json
copy

SYNOPSIS

go vet [build flags] [-vettool prog] [vet flags] [pkg...]

PARAMETERS

-vettool=prog
    override internal vet tool with custom program prog

-all
    enable all available analyzers (non-default ones)

-methods=obj
    check method sets for obj receiver types

-printf=true|false
    check printf-like format verbs (default true)

-assign=true|false
    check effectiveness of struct assignments

-atomic=true|false
    check atomic operations usage

-bools=true|false
    check bool operations

-buildtag=true|false
    check build tag syntax

-cgocall=true|false
    detect invalid cgo calls

-composites=true|false
    check composite literal permissions

-errorsas=true|false
    check errors.As calls

-ifaceassert=true|false
    check interface type assertions

-loopclosure=true|false
    check loop closure captures

-nilfunc=true|false
    check nil function calls

-shift=true|false
    check shift count overflow

-unreachable=true|false
    check unreachable code

-unparam=true|false
    check unused function parameters

pkg...
    import paths of packages to vet

DESCRIPTION

go vet is a static analysis tool in the Go programming language toolchain. It scans Go source code for common errors and suspicious constructs, such as mismatched printf arguments, redundant assignments, misuse of atomic operations, or unsafe interface assertions. Unlike a compiler, which focuses on syntax and types, vet applies heuristics to detect subtle bugs that could lead to runtime issues.

Vet runs multiple lightweight analyzers by default, covering printf formatting, composite literals, error handling, loop closures, and more. Users specify packages via import paths; if omitted, it analyzes the current package. It integrates seamlessly with go build workflows and supports build flags for environment control.

While effective for quick checks, vet may produce false positives or miss complex issues. It complements code reviews, testing, and tools like fuzzing, but is not exhaustive. Output includes file, line, and detailed explanations for flagged issues. Run with -v for verbose progress.

CAVEATS

Heuristics may cause false positives/negatives; not all bugs detected. Requires Go toolchain installed. Analyzed code must compile cleanly.

DEFAULT ANALYZERS

Enabled by default: assign, atomic, bools, buildtag, cgocall, composites, errorsas, ifaceassert, loopclosure, lostcancel, nilfunc, printf, shift, sigs, structs, unreachable, unparam, unusedresult (Go 1.22+)

EXIT STATUS

0 if no issues or only warnings; non-zero if suspicious code found.

HISTORY

Introduced as go tool vet in Go 1.0 (2012); go vet subcommand stabilized in Go 1.2. Analyzers expanded over releases, e.g., loopclosure in 1.8, lostcancel in 1.15. Default set refined in Go 1.14+ for broader coverage.

SEE ALSO

go(1), go build(1), go test(1), gofmt(1)

Copied to clipboard