Lesson 22 / 26

go vet

Catch suspicious code the compiler accepts.

Static checks for common mistakes

go vet reports code that compiles but is probably wrong: Printf verbs that do not match their arguments, unreachable code, copying locks, misuse of struct tags, suspicious shifts, and more. go test runs a subset of vet checks automatically. Linters such as staticcheck and golangci-lint add many more checks; run them in CI.

A format-verb mistake caught by vet, run

I ran this with Go 1.27.1 (go vet . 2>&1 in a module named demo, standard library only). Printf with %d for a string compiles, but vet reports the wrong argument type with its file and line.

package main

import "fmt"

func main() {
	name := "Asha"
	fmt.Printf("hello %d\n", name) // wrong verb for a string
	var unused = 1
	_ = unused
}

Output:

main.go:7:20: fmt.Printf format %d has arg name of wrong type string

Add staticcheck

staticcheck finds deprecated APIs, unused results and subtle bugs beyond vet; most teams run it via golangci-lint.

Quick check: What does go vet report?

  • Dependency updates
  • Only syntax errors
  • Performance benchmarks
  • Code that compiles but is likely a bug
Answer

Code that compiles but is likely a bug — Static analysis for common mistakes.