go vet, staticcheck and CI
The compiler already rejects a program that does not type check, which is more than a C compiler does. What it cannot reject is a program that type checks and is still wrong: a format verb that does not match its argument, a struct tag with a missing quote, a lock copied by value. go vet is the set of analyzers in the toolchain that look for exactly that, and the two linters outside the toolchain look for more.
go vet
The package below has two mistakes a compiler accepts. The first prints a string with a number verb, the second has a struct tag with no closing quote, so encoding/json would ignore the field and a reader would wonder why the name is empty.
package lint
import "fmt"
// Report prints a count.
func Report(n int) {
fmt.Printf("%d\n", "many")
}
// Count formats a count.
func Count(n int) string {
return fmt.Sprintf("%d", n)
}
// Config carries the name of a report.
type Config struct {
Name string `json:"name`
}
go vet ./...; echo "status: $?"
lint/lint.go:7:14: fmt.Printf format %d has arg "many" of wrong type string
lint/lint.go:17:2: struct field tag `json:"name` not compatible with reflect.StructTag.Get: bad syntax for struct tag value
status: 1
Each line is file:line:column: message, which every editor and every CI annotator understands without a plugin. The exit status is 1, so a script that runs vet and stops on a non-zero status blocks the build. Vet is not a style checker: it does not care about the name of a variable or the length of a function, and a clean vet says nothing about either.
What go test runs
go test runs a subset of vet before it builds the test binary, so a printf mistake stops the tests before a single one runs. The subset is the checks that are almost never wrong (printf, stringintconv, tests, atomic, bool, buildtags, errorsas, ifaceassert, nilfunc, directive), and the struct tag is not in it:
go test ./...
# example.com/lintcheck/lint
# [example.com/lintcheck/lint]
lint/lint.go:7:14: fmt.Printf format %d has arg "many" of wrong type string
FAIL example.com/lintcheck/lint [build failed]
FAIL
The word build failed is exact: the vet finding is reported as a build failure, the test binary is never linked, and the coverage of this run would be meaningless. A CI job that runs only go test therefore gets the printf check for free and still needs go vet ./... for the rest.
The fix
Two lines: the verb takes the argument, and the tag gets its closing quote. Nothing about the shape of the program changes, which is the kind of repair vet is good at.
package lint
import "fmt"
// Report prints a count.
func Report(n int) {
fmt.Printf("%d\n", n)
}
// Count formats a count.
func Count(n int) string {
return fmt.Sprintf("%d", n)
}
// Config carries the name of a report.
type Config struct {
Name string `json:"name"`
}
The package has one test, and it is the reason the coverage at the bottom of this page is not zero:
package lint
import "testing"
func TestCount(t *testing.T) {
if got, want := Count(7), "7"; got != want {
t.Errorf("Count(7) = %q, want %q", got, want)
}
}
gofmt -l .
go vet ./...; echo "status: $?"
status: 0
gofmt -l . printed no file name, so every file is already formatted. That is the first step of the script below, and the reason is practical: a diff that mixes formatting with a change is a diff nobody can review.
staticcheck and golangci-lint
Neither tool is installed in this image, and this page shows their commands without their output, because an invented line would be worse than no line. staticcheck is the strict analyzer set that the Go team does not ship: unused code, a deprecated call, a nil check that is always true, a loop that can be a slice expression. golangci-lint runs staticcheck and a dozen other linters in one pass with one config file.
go install honnef.co/go/tools/cmd/staticcheck@latest
staticcheck ./...
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
golangci-lint run ./...
Both are Go programs, so go install puts them in GOBIN and no package manager is involved. Where they are installed, the order in CI is gofmt, then vet, then the linter, then tests: cheapest and most certain first, so a typo does not wait for a five-minute test run to be reported.
A CI script that runs here
A CI job is a shell script with a checkout in front of it, so the script is the part worth testing. This one fails on unformatted code before it compiles anything, and its every step ran in this container:
#!/bin/sh set -eu if [ -n "$(gofmt -l .)" ]; then echo "gofmt wants to change:" gofmt -l . exit 1 fi echo "gofmt: clean" go vet ./... echo "vet: clean" go test -count=1 -race ./... go test -coverprofile=/tmp/cover.out ./... > /dev/null go tool cover -func=/tmp/cover.out | tail -1
sh ci.sh
gofmt: clean
vet: clean
ok example.com/lintcheck/lint 1.022s
total: (statements) 50.0%
set -eu is what makes the script a gate: without it a failing go vet would be followed by a passing go test and the script would exit 0. The coverage number is 50 percent because Report has no test, and the page prints it rather than hiding the gap.
The same steps in GitHub Actions
The file below is a sketch, not a run: this container has no runner and no network to GitHub, so its syntax was not checked by GitHub. The steps in it are the steps of the script above, which did run.
name: test on: [push, pull_request] jobs: test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: actions/setup-go@v5 with: go-version: '1.26' - run: gofmt -l . - run: go vet ./... - run: go test -race -coverprofile=cover.out ./... - run: go tool cover -func=cover.out | tail -1
Two details decide whether such a job is useful. setup-go caches the module download and the build cache by the hash of go.sum, which is the difference between a forty-second and a four-minute job. And the version in the workflow must be the version the project develops with, or a language feature that is new in that version compiles on a laptop and fails in CI.
The lesson on go vet, staticcheck and golangci-lint goes through the analyzers one by one, and CI for a Go project builds the workflow file for a real repository.
| Tool | Catches | In this image |
|---|---|---|
| gofmt -l | Formatting, in one line of shell | Yes, run above |
| go vet ./... | Format verbs, struct tags, copied locks, lost cancels | Yes, run above |
| staticcheck ./... | Unused code, always-true checks, deprecated calls | No, command only |
| golangci-lint run | staticcheck and the other linters under one config | No, command only |
| go test -race | Data races the run actually hit | Yes, run above |
The code on this page was run with go 1.26.10 on Alpine Linux 3.24.3 (x86_64) in a Docker container. staticcheck and golangci-lint are absent from that image, and no output of theirs appears on the page.
Article author: Arthur Isaev