Go 1 cheatsheet

Go 1: Testing and profiling

10 common Go 1 tasks.

Run all tests

Run the tests in every package of the module.

go test ./...

Run tests verbosely

Print the name and result of every test.

go test -v ./...

Run matching tests

Run tests whose names match a regular expression, in one package.

go test -run TestLogin ./pkg/auth

Detect data races

Run the tests with the race detector turned on.

go test -race ./...

Bypass the test cache

Run tests again even if nothing changed.

go test -count=1 ./...

Measure coverage

Print the percentage of statements covered by tests.

go test -cover ./...

View coverage in the browser

Write a coverage profile and open an annotated HTML report.

go test -coverprofile=coverage.out ./... && go tool cover -html=coverage.out

Run benchmarks

Run benchmarks and report memory allocations. Tests are skipped.

go test -bench=. -benchmem -run=^$ ./...

Fuzz a function

Run a fuzz test for 30 seconds to look for inputs that make it fail.

go test -fuzz=FuzzParse -fuzztime=30s

Profile a running service

Capture 30 seconds of CPU profile from a service that exposes net/http/pprof.

go tool pprof http://localhost:6060/debug/pprof/profile

Was this page helpful?