15. Quick Reference Commands

Senior-ready CLI snippets for test/bench, linters, and vulnerability checks.

Question: What are some essential Go commands you use for testing, profiling, and validation?

Answer:

# Run all tests with the race detector and generate a coverage report
go test ./... -race -cover -coverprofile=cover.out && go tool cover -func=cover.out

# Run benchmarks, collecting CPU and memory profiles
go test -run=NONE -bench=. -benchmem -cpuprofile cpu.out -memprofile mem.out ./...

# Run a comprehensive suite of linters
golangci-lint run ./...

# Check for known vulnerabilities in your code's call graph
govulncheck ./...
# Quick pprof/trace against a running service
go tool pprof -http=:0 http://localhost:6060/debug/pprof/profile?seconds=30
go tool pprof -http=:0 http://localhost:6060/debug/pprof/heap
go tool trace http://localhost:6060/debug/pprof/trace?seconds=5

# Module hygiene and workspaces
go mod verify && go mod graph | head -n 20
go work init && go work use ./...

# Sign and verify container/image or artifact provenance (example)
# cosign sign --key cosign.key <image> && cosign verify --key cosign.pub <image>