13. Handy Commands

CLI recipes for tests, profiling, modules, docs, cross-compiling, and static analysis.

go test ./... -race -count=1
go test -run TestName -bench=. -benchmem ./...
go tool pprof -http=:0 cpu.out
go list -m -u all
go mod tidy && git diff --exit-code go.mod go.sum
go tool cover -func=coverage.out
go env GOPATH GOMOD CACHE
go doc net/http | cat
go vet ./...
# staticcheck (install first): go install honnef.co/go/tools/cmd/staticcheck@latest && staticcheck ./...
# cross-compile
GOOS=linux GOARCH=amd64 go build -o app-linux-amd64 ./cmd/app
# escape analysis
go build -gcflags='-m' ./...

Extra commands

# Race-safe tests with coverage summary
go test ./... -race -cover -count=1

# Benchmarks with allocation counts
go test -bench=. -benchmem ./...

# Tidy and verify modules
go mod tidy && go mod verify

# Show why a module version is selected
go mod graph | grep your-dep