go vet, staticcheck and golangci-lint

go vet is the static analysis the toolchain ships: it builds the package and checks a set of patterns that are almost always bugs, from a format verb that does not match its argument to a copied mutex. It runs as part of go test, so a test suite fails on a vet finding unless it is switched off. staticcheck and golangci-lint are separate tools that go further; the previous lesson measured a program, these check it.

What go vet catches

The program below has three findings: a %d verb with a string argument, a %w verb with an int, and a fmt.Errorf result that is discarded. The compiler accepts all three, because Printf and Errorf take variadic any arguments; vet reads the format string and the argument types and reports the mismatch before the program runs.

package main import "fmt" func main() { fmt.Printf("count: %d\n", "twelve") fmt.Errorf("wrap: %w", 42) fmt.Println("done") }

go vet ./...

main.go:6:21: fmt.Printf format %d has arg "twelve" of wrong type string
main.go:7:20: fmt.Errorf format %w has arg 42 of wrong type int
main.go:7:2: result of fmt.Errorf call not used

Each line names the file, the position, the problem and the type it received, and the unused-error line comes from the same analysis. The exit status of go vet is 1 when it reports anything, which is why it fits a CI step: a finding is a failure, and there is no output to parse. The fixed program passes in silence.

package main import ( "errors" "fmt" ) var ErrBad = errors.New("bad value") func main() { count := 12 fmt.Printf("count: %d\n", count) err := fmt.Errorf("wrap: %w", ErrBad) fmt.Println(err) }

go vet ./... && echo vet: clean

vet: clean

Vet is deliberately conservative: it reports only patterns it is sure about, which is why it is safe to enable everywhere and why it does not find most style problems. The lock-copy check it also has reports passing a sync.Mutex or a sync.WaitGroup by value, a mistake that compiles and breaks the synchronisation at run time.

staticcheck

staticcheck is a third-party analyser with a much larger set of checks: an ineffective assignment, a nil check on a value that cannot be nil, a deprecated standard-library call, an error that is created and discarded, a loop that never runs. It is installed with go install honnef.co/go/tools/cmd/staticcheck@latest and run as staticcheck ./.... Its findings are advisory in a way vet's are not, so a project usually adopts it with a list of accepted checks rather than a blanket switch.

golangci-lint

golangci-lint is a runner that executes many linters, staticcheck among them, with one configuration file and one output format. A project tunes it in .golangci.yml: enable the linters it wants, disable the noisy ones, and set a timeout. It is the usual way a large repository applies the same rules in every editor and in CI, because a single binary and a single config beat a list of tools in a shell script.

This container has neither tool installed, so the page shows no output from them; the commands above are the ones to run where they are. That is a deliberate limit of this environment rather than a statement about the tools: vet is part of the toolchain and runs here, staticcheck and golangci-lint are separate binaries and do not.

Wiring the checks into CI

The minimum is go vet ./... and go test ./... on every push, because both use the toolchain the project already pins. Add staticcheck or golangci-lint when the team agrees on their rules, pin the version in the CI file, and fail the job on a finding: a lint step that only prints is a step nobody reads. Keep the analysis on the same Go version as the build, or the two disagree about what is deprecated.

Exercises

  1. Add a third misuse to the bad program, such as fmt.Sprintf("%s", 42), and read the vet message. Which tool catches it first, the compiler or vet?
  2. Copy a sync.Mutex into a function and run vet. What does the message name, and why does the compiler accept the program?
  3. Run staticcheck on a package of your own and group its findings by check code. Which group would you enable first, and which would you accept?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What is go vet?

    Answer

    Static analysis shipped with the Go toolchain

    It is part of the toolchain and runs inside go test.

  2. Why does a format-string mismatch compile?

    Answer

    Printf takes variadic any arguments, so the types are not checked by the compiler

    Vet reads the constant format string and the argument types instead.

  3. What does vet report for a copied sync.Mutex?

    Answer

    A lock value passed by value

    The copylocks analyser catches a lock that is copied and therefore useless.

  4. What does staticcheck add over vet?

    Answer

    A much larger set of advisory checks

    It is a separate analyser with style and bug checks vet does not have.

  5. What is golangci-lint?

    Answer

    A runner for many linters with one configuration

    It runs staticcheck and others and reports in one format.

  6. Where do vet findings surface besides a direct run?

    Answer

    In go test, which runs vet on the package

    A test suite fails on a vet finding unless the check is disabled.

  7. What should a CI lint step do with a finding?

    Answer

    Fail the job

    A step that only prints is a step nobody reads.

  8. Why pin the linter version in CI?

    Answer

    New versions add checks and would fail unchanged code

    A pinned version keeps the gate stable across releases.

  9. Which check is vet known to be conservative about?

    Answer

    It reports only patterns it is sure about, so it misses most style issues

    That conservatism is why it is safe to enable by default.

  10. What does this container have installed?

    Answer

    go vet, but not staticcheck or golangci-lint

    The page is explicit about that limit instead of inventing output.

The code on this page was run with go 1.26.10 on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev