Go
Modules and dependencies, building and cross-compiling, tests with race detector and coverage, profiling.
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 tidyWhy a dependency is neededadvanced
go mod why -m <go_package>
Available updates for all dependenciesadvanced
go list -m -u allBuild and run
Run
go run .Build a binary
go build -o bin/app ./cmd/appCross-compile for Linux ARM64
GOOS=linux GOARCH=arm64 go build -o bin/app-linux-arm64 ./cmd/appSmall static binary (for scratch/distroless images)advanced
CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" -o bin/app ./cmd/appInstall 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.outBenchmarks 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=30Goroutine dumpadvanced
curl -s http://localhost:6060/debug/pprof/goroutine?debug=2