$ command-helper

Go

Modules and dependencies, building and cross-compiling, tests with race detector and coverage, profiling.

Requires Google sign-in

Modules

New module
go mod init <module_path>
Add or upgrade a dependency
go get <go_package>@latest
Remove unused, add missing dependencies
go mod tidy
Why a dependency is neededadvanced
go mod why -m <go_package>
Available updates for all dependenciesadvanced
go list -m -u all

Build and run

Run
go run .
Build a binary
go build -o bin/app ./cmd/app
Cross-compile for Linux ARM64
GOOS=linux GOARCH=arm64 go build -o bin/app-linux-arm64 ./cmd/app
Small static binary (for scratch/distroless images)advanced
CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" -o bin/app ./cmd/app
Install a CLI tool
go install <go_package>@latest

Tests and quality

All tests with the race detector
go test -race ./...
One test by name, verbose
go test -run '^<test_method>$' -v ./...
Coverage report in the browser
go test -coverprofile=cover.out ./... && go tool cover -html=cover.out
Benchmarks with memory statsadvanced
go test -bench=. -benchmem ./...
Format and vet
gofmt -w . && go vet ./...

Profiling

CPU profile of a running service (net/http/pprof)advanced
go tool pprof -http=:8081 http://localhost:6060/debug/pprof/profile?seconds=30
Goroutine dumpadvanced
curl -s http://localhost:6060/debug/pprof/goroutine?debug=2