Skip to content
Tutorials Center
My learning

Topics

Go Reference ยท The go Command and Tools

The go vet command

Reference for go vet: what it checks (printf, copylocks, loopclosure, unusedresult and more), how to run single analyzers, output flags and vettool.

go vet examines Go source code and reports suspicious constructs that compile fine but are almost certainly bugs: a Printf call whose verbs do not match its arguments, a mutex copied by value, a struct tag with a typo, unreachable code. It does not find every bug, and it avoids style opinions; the checks are chosen to have very few false positives.

Vet is a set of analyzers built on the golang.org/x/tools/go/analysis framework. go test runs a small, high-confidence subset of them automatically before every test run, so a vet failure can also appear as a test failure.

Terminal
go vet ./...                   # every analyzer, every package
go vet -printf ./...           # only the printf analyzer
go vet -copylocks=false ./...  # everything except copylocks
go vet -json ./...             # machine-readable diagnostics

Try it

The snippets run on the site's compiler, which accepts code that vet would reject. The first shows the runtime damage vet prevents; the second builds a tiny analyzer with the same parsing packages vet uses.

Go Bugs that compile but vet would flag

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Go A mini-vet: checking Printf argument counts

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Syntax

Terminal
go vet [build flags] [vet flags] [packages]
go vet -vettool=/path/to/tool [packages]
Common analyzer Finds
printf Format strings that do not match their arguments, in fmt, log, testing and your own printf-like wrappers.
copylocks Values containing a sync.Mutex, WaitGroup or similar that are copied (value receivers, assignments, range loops).
loopclosure Closures started with go or defer that capture a loop variable in a way that is still wrong (less common since Go 1.22 gave each iteration its own variable).
lostcancel A context.CancelFunc from WithCancel or WithTimeout that is never called.
unusedresult Ignored results of pure functions such as fmt.Sprintf or errors.New.
structtag Malformed struct tags such as json:"name or duplicate JSON names.
assign, bools, nilfunc, unreachable Self-assignments, redundant or impossible boolean conditions, comparing a function to nil, code after return or panic.
atomic, unsafeptr, stdmethods, tests, httpresponse, stringintconv, errorsas, slog, waitgroup Misuse of sync/atomic, invalid unsafe.Pointer conversions, wrong signatures for String/MarshalJSON, malformed test names, using an HTTP response before checking the error, string(int) conversions, wrong errors.As targets, bad log/slog key-value pairs, and (Go 1.25+) WaitGroup.Add called inside the goroutine it counts.
Flag Description
-name / -name=false Enable only this analyzer (any -name turns the others off), or disable just this one.
-json Print diagnostics as JSON.
-c=N Show N lines of source context around each finding.
-vettool=path Run a different analyzer binary, for example one built with multichecker or golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow.

go doc cmd/vet documents the analyzers, and go vet -help lists the flags of the installed version.

Exit status: nonzero when it prints diagnostics or the package fails to build. Output is file:line:col: message.

Usage notes

  • Vet needs a compiling package. If your code has type errors, you get compile errors instead of vet reports.
  • Run it in CI with go vet ./.... Because it is part of the toolchain, it needs no extra install and always matches your Go version.
  • Printf wrappers are detected. If your function forwards its format and ...any arguments to fmt.Sprintf, vet infers that it is printf-like and checks callers too. Name such helpers with an f suffix by convention.
  • Silencing. There is no ignore comment. Fix the code, or disable one analyzer with -name=false. Generated files are skipped (those with a // Code generated ... DO NOT EDIT. line).
  • Vet versus linters. Vet is deliberately small. Tools such as staticcheck and golangci-lint check many more patterns and style rules; see Vet and Linting.
  • The race detector is separate. Data races are found at runtime with -race, not by vet (copylocks only catches one cause).
  • Build tags matter. Vet analyzes the files selected by the current GOOS, GOARCH and -tags, so run it for each target you support.
  • Go 1.22 loop variables. With go 1.22 or later in go.mod, each loop iteration has its own variable, which removes most loopclosure findings.