CLI Cheatsheet
Mental map
Create go mod init
Dependencies go get / go mod tidy
Run go run
Compile go build
Install go install
Format go fmt
Analyse go vet
Test go test
Inspect go list / go doc / go env
Generate go generate
Clean go clean
Profile go tool pprof
Workspace go workThe complete command list is available through:
go helpFor a specific command:
go help <command>Starting a project
| Command | Purpose |
|---|---|
go mod init <module> | Create a new Go module |
go mod tidy | Add missing dependencies and remove unused ones |
go mod download | Download dependencies without building |
go mod verify | Check downloaded dependencies have not changed |
go mod init github.com/ben/example
go mod tidyA module is normally an entire project or repository. The command creates a go.mod file.
Running and building
| Command | Purpose |
|---|---|
go run . | Compile and run the current package |
go run main.go | Run one specific file |
go build | Compile the current package |
go build ./... | Build every package in the module |
go build -o app | Build a binary with a chosen name |
go install | Compile and install the current program |
go run .
go build -o myapp
./myappgo run uses a temporary binary. go build creates a binary but does not install it. (Go Packages)
Testing
| Command | Purpose |
|---|---|
go test | Test the current package |
go test ./... | Test all packages |
go test -v ./... | Show individual test results |
go test -run TestName | Run matching tests |
go test -count=1 | Disable cached test results |
go test -race ./... | Detect data races |
go test -cover ./... | Show test coverage |
go test -coverprofile=cover.out ./... | Save coverage data |
go test -v -run TestWalkRun one subtest:
go test -run 'TestWalk/structs'Open an HTML coverage report:
go test -coverprofile=cover.out ./...
go tool cover -html=cover.outBenchmarks
go test -bench=.
go test -bench=BenchmarkWalk
go test -bench=. -benchmembenchmem includes allocation statistics.
Fuzz tests
go test -fuzz=FuzzName
go test -fuzz=FuzzName -fuzztime=30sFormatting and checking
| Command | Purpose |
|---|---|
go fmt ./... | Format all Go packages |
gofmt -w . | Format files directly |
go vet ./... | Find suspicious code and likely mistakes |
go test ./... | Compile, vet and test code |
go fix ./... | Update code using Go’s automatic fixes |
go fmt ./...
go vet ./...
go test ./...go vet does not prove that code is correct. It finds suspicious patterns, such as incorrect formatting arguments, broken struct tags and copied lock values. go test automatically runs a high-confidence subset of vet checks. (Go Packages)
Inspect available vet checks:
go tool vet help
go tool vet help printfDependencies
| Command | Purpose |
|---|---|
go get example.com/pkg | Add or update a dependency |
go get example.com/pkg@v1.2.3 | Request a specific version |
go get example.com/pkg@latest | Request the latest version |
go get example.com/pkg@none | Remove a dependency |
go list -m all | List all modules in the dependency graph |
go mod graph | Print the module dependency graph |
go mod why <module> | Explain why a dependency is required |
go get github.com/google/uuid@latest
go mod tidyUse go get for dependencies used by your module.
Use go install to install command-line tools:
go install golang.org/x/tools/cmd/stringer@latestModern Go separates installing executables from changing project dependencies. (Go)
Listing and inspecting
| Command | Purpose |
|---|---|
go list | List the current package |
go list ./... | List every package |
go list -m all | List modules |
go env | Show Go environment settings |
go version | Show the installed Go version |
go doc <name> | Read documentation in the terminal |
go help <command> | Read help for a Go command |
go doc fmt.Println
go doc http.Server
go help test
go help modulesUseful environment checks:
go env GOPATH
go env GOMOD
go env GOWORK
go env GOOS
go env GOARCHChange a persistent Go setting:
go env -w GOPROXY=https://proxy.golang.org,directUndo it:
go env -u GOPROXYInstalling tools
go install <module>/cmd/<tool>@<version>Example:
go install golang.org/x/tools/cmd/goimports@latestInstalled binaries normally go into:
go env GOBINWhen GOBIN is empty, Go normally uses:
$(go env GOPATH)/binCode generation
Run all //go:generate directives:
go generate ./...Example directive:
//go:generate stringer -type=StatusThen run:
go generatego generate is not run automatically by go build or go test. (Go Packages)
Cleaning caches
| Command | Purpose |
|---|---|
go clean | Remove generated build files |
go clean -cache | Clear the build cache |
go clean -testcache | Clear cached test results |
go clean -modcache | Delete all downloaded modules |
go clean -fuzzcache | Clear cached fuzzing results |
go clean -testcacheBe careful with:
go clean -modcacheIt deletes the entire module download cache, so dependencies must be downloaded again. (Go Packages)
Workspaces
Workspaces allow several local modules to be developed together.
go work init ./api ./sharedAdd another module:
go work use ./workerSynchronise workspace dependency versions:
go work syncInspect whether workspace mode is active:
go env GOWORKA workspace creates a go.work file.
Cross-compilation
Build for Linux:
GOOS=linux GOARCH=amd64 go buildBuild for Apple Silicon:
GOOS=darwin GOARCH=arm64 go buildBuild for Windows:
GOOS=windows GOARCH=amd64 go build -o app.exeCommon values:
| Variable | Examples |
|---|---|
GOOS | linux, windows, darwin |
GOARCH | amd64, arm64, 386 |
List supported combinations:
go tool dist listBuild flags
Show executed commands
go build -xPrint commands without running them
go build -nForce a complete rebuild
go build -aInclude build tags
go build -tags integration
go test -tags integration ./...Reduce binary size
go build -ldflags="-s -w"Insert build information
go build -ldflags="-X 'main.version=1.2.3'"The Go variable must exist:
package main
var version = "development"Debugging and profiling tools
View compiler escape analysis
go build -gcflags="-m"More detail:
go build -gcflags="-m=2"This explains which values escape to the heap.
CPU profiling
go test -cpuprofile=cpu.out
go tool pprof cpu.outMemory profiling
go test -memprofile=memory.out
go tool pprof memory.outOpen the browser interface:
go tool pprof -http=:8080 cpu.outInspect a binary
go tool nm ./myapp
go tool objdump ./myappSome lower-level tools are accessed through go tool, including pprof, cover, vet, compile and objdump. (Go)
Vendor dependencies
Create a local vendor directory:
go mod vendorBuild using vendored dependencies:
go build -mod=vendorCheck vendored packages:
go list -mod=vendor allThis is useful for controlled or offline builds.
Useful everyday sequence
New project
mkdir my-project
cd my-project
go mod init github.com/ben/my-project
go test ./...
go run .Before committing
go fmt ./...
go vet ./...
go test -race ./...
go mod tidyDiagnose strange cached tests
go clean -testcache
go test -count=1 -v ./...Full basic health check
go mod tidy
go fmt ./...
go vet ./...
go test -race -cover ./...
go build ./...