Code Quality Basics
10 examples to get you started with Go code quality - 7 basic and 3 intermediate.
Search across all documentation pages
10 examples to get you started with Go code quality - 7 basic and 3 intermediate.
mkdir quality && cd quality && go mod init example.com/quality.go install golang.org/x/tools/cmd/goimports@latest and go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest.Rewrite a file to canonical Go formatting.
gofmt -w main.gopackage main
import "fmt"
func main() {fmt.Println("hello")}After gofmt -w, spacing and indentation match community style.
gofmt is deterministic: same input always yields same output.test -z "$(gofmt -l .)" to fail on drift.Related: gofmt & goimports Enforcement - team formatting policy
Format and fix import blocks in one step.
goimports -w .package main
import "fmt"
func main() {
fmt.Println(time.Now()) // missing import before goimports
}-local example.com/quality to group your module paths separately.Related: gofmt & goimports Enforcement -
-localgrouping
Run built-in analyzers across the module.
go vet ./...package main
import "fmt"
func main() {
fmt.Printf("%d", "not-a-number") // vet: Printf format mismatch
}Related: go vet & Common Analyzer Diagnostics - common messages
Quality includes behavior, not just static checks.
go test ./...package add
func Sum(a, b int) int { return a + b }package add_test
import "testing"
func TestSum(t *testing.T) {
if got := add.Sum(2, 3); got != 5 {
t.Fatalf("got %d", got)
}
}*_test.go files in the same package (or _test external package).go test compiles tests with the race detector when you pass -race.Related: CI Quality Gates: Lint, Test, Race, Vuln - full pipeline
Check formatting without writing files (CI-friendly).
gofmt -l ..go file is formatted.gofmt -w.git diff --exit-code after format in pre-commit hooks.Related: Pre-commit Hooks & Review Checklists - local automation
Run the standard linter bundle.
golangci-lint run ./....golangci.yml after baseline is green.Related: golangci-lint Configuration - config file patterns
One command for contributors.
.PHONY: check
check:
gofmt -l . | tee /tmp/gofmt.out && test ! -s /tmp/gofmt.out
go vet ./...
go test ./...make checkmake check in README and CONTRIBUTING.golangci-lint run when the team is ready.Related: Linting & Code Quality Best Practices - adoption pacing
Catch data races that vet misses.
go test -race ./...package counter
import "sync"
type Counter struct {
mu sync.Mutex
n int
}
func (c *Counter) Inc() {
c.mu.Lock()
c.n++
c.mu.Unlock()
}-race slows tests and needs more memory; run in CI, not every save.Related: CI Quality Gates: Lint, Test, Race, Vuln - when to require race
Keep module metadata consistent.
go mod tidy
git diff --exit-code go.mod go.sumgo.sum updates.govulncheck for security scanning on the tidy graph.Related: govulncheck for Dependency Vulnerabilities - CVE scanning
Block commits that fail fast checks.
#!/usr/bin/env bash
set -euo pipefail
gofmt -l . | grep . && { echo "run gofmt -w"; exit 1; } || true
go vet ./...
go test ./...chmod +x scripts/pre-commit.sh
# wire with: ln -s ../../scripts/pre-commit.sh .git/hooks/pre-commit--no-verify) for emergencies only.Related: Pre-commit Hooks & Review Checklists - hook templates
Stack versions: This page was written for Go 1.26.x (Green Tea GC default, go fix modernizers - verify patch at build), chi (latest - verify at build), gin (latest - verify at build), echo (latest - verify at build), google.golang.org/grpc (latest - verify at build), sigs.k8s.io/controller-runtime (latest - verify at build), kubebuilder (latest - verify at build), tinygo (latest - verify board targets at build), wazero (latest - verify at build), and golangci-lint (latest - verify linter set at build).
Reviewed by Chris St. John·Last updated Jul 19, 2026