Go Go toolchain commands

Version 1.24.4 · source

Start here

go mod init <module>
Start a new module
go run .
Compile and run the main package
go build ./...
Compile every package
go test ./...
Run all tests
go fmt ./...
Format all sources
go vet ./...
Report suspicious code
go get <pkg>@<version>
Add or change a dependency
go mod tidy
Sync go.mod with imports

Concepts

Module go.mod, versioned unit
Package one directory, one import path
.go file declarations; _test.go for tests
Module, package and file

Build and run

go run .
Compile and run the package here
go run <file.go> <args>
Run files, passing program args
Arguments after the package or files go to your program, not to go. A literal -- is passed on too.
go build ./...
Compile all packages, discard results
go build -o <path> .
Write the binary to a chosen path
go install <pkg>@latest
Install a tool binary into $GOBIN
go build -race
Build with the race detector
go build -ldflags="-s -w"
Strip symbols and debug info
go build -ldflags="-X main.version=<v>"
Set a string variable at link time
go build -tags <tag,...>
Enable build tags
go build -trimpath
Remove local paths from the binary
go build -v
List packages as they compile
go build -n
Print build commands, do not run
go build -x
Print build commands while running
go build -a
Rebuild even up-to-date packages
go build -C <dir>
Change directory first
Must be the first flag on the command line.
go tool nm <binary>
List symbols in a binary
go clean -cache
Delete the build cache
go clean -modcache
Delete downloaded modules
go clean -testcache
Expire cached test results

Cross-compiling

GOOS=<os> GOARCH=<arch> go build
Build for another platform
go tool dist list
List supported GOOS/GOARCH pairs
CGO_ENABLED=0 go build
Static build without C toolchain
GOOS=linux GOARCH=arm64 go build -o <name>
Example: Linux on ARM64

Testing

go test
Test the package in the current dir
go test ./...
Test every package below here
go test -v
Print each test and its output
go test -run <regexp>
Run only matching tests
Anchor it: -run '^TestFoo$'. Use / to match subtests: -run 'TestFoo/case'.
go test -count=1
Bypass the test result cache
Package-list runs cache passing results; any -count disables that, 1 is the idiom.
go test -race
Run with the race detector
go test -short
Skip tests that check testing.Short()
go test -failfast
Stop after the first failure
go test -timeout <d>
Abort a test binary after d
Default is 10m.
go test -skip <regexp>
Skip matching tests
go test -shuffle=on
Randomise test order
go test -parallel <n>
Max parallel tests (t.Parallel)
go test -list <regexp>
List matching tests, do not run
go test -json
Machine-readable event stream
go test -vet=off
Skip the built-in vet pass
go test -cover
Show statement coverage
go test -coverprofile=<file>
Write a coverage profile
go tool cover -html=<file>
Browse coverage in a browser
go tool cover -func=<file>
Coverage per function
go test -bench <regexp>
Run benchmarks (. for all)
Benchmarks run after the tests; add -run '^$' to skip those.
go test -benchmem
Add allocation stats to benchmarks
go test -cpuprofile <file>
Write a CPU profile
go tool pprof <file>
Inspect a profile interactively
go test -fuzz <regexp>
Fuzz one target, one package at a time
go test -c
Compile the test binary, do not run

Format and check

go fmt ./...
Format files with gofmt -l -w
go vet ./...
Find likely mistakes
go doc <pkg>[.<Symbol>]
Show docs for a package or symbol
go doc -all <pkg>
Show every exported item
go doc -src <pkg>.<Symbol>
Show the source of a symbol
go fix ./...
Rewrite code to use newer APIs
go generate ./...
Run //go:generate directives
Never runs automatically during build or test.
go bug
Open a bug report prefilled with env

Dependencies

go get <pkg>
Add a dependency at the latest version
go get <pkg>@<version>
Pin a version, branch or commit
go get <pkg>@none
Remove a dependency
go get -u ./...
Upgrade deps of this module
go get -u=patch ./...
Upgrade to newer patch releases only
go get go@<version>
Raise the go directive
go get -tool <pkg>
Track a tool dependency in go.mod
Go 1.24+; run it later with go tool <name>.
go get -t ./...
Include test dependencies
go list -m all
List all modules in the build
go list -m -u all
Show modules with available updates
go list -m -versions <module>
List known versions of a module
go list ./...
List package paths below here
go list -f '{{.Dir}}' <pkg>
Format output with a template
go list -json <pkg>
Package info as JSON
go list -deps <pkg>
Include transitive dependencies
go mod why <pkg>
Why a package is needed
go mod graph
Print the module requirement graph

Modules

go mod init <module>
Create go.mod here
go mod tidy
Add missing, remove unused modules
go mod tidy -diff
Show tidy changes, exit 1 if any
Handy in CI.
go mod download
Fetch modules into the cache
go mod verify
Check cached modules against go.sum
go mod vendor
Copy dependencies to vendor/
go build -mod=vendor
Build from vendor/ explicitly
Used by default when vendor/ exists and go.mod says go 1.14 or later.
go mod edit -replace <old>=<new>
Add a replace directive
go mod edit -require <path>@<v>
Edit a requirement without fetching
go mod edit -go=<version>
Set the go directive
go mod edit -json
Dump go.mod as JSON
go mod edit -fmt
Reformat go.mod

Workspaces

go work init <dir>...
Create go.work for local modules
go work use <dir>
Add a module to the workspace
go work use -r .
Add every module below here
go work sync
Sync workspace deps to the modules
go work edit -replace <old>=<new>
Replace in go.work
go env GOWORK
Show which go.work is active
GOWORK=off go <cmd>
Ignore the workspace once

Environment

go version
Print the Go toolchain version
go version -m <binary>
Show Go and module versions in a binary
go env
Print all Go environment settings
go env <VAR>...
Print specific settings
go env -w <VAR>=<value>
Persist a setting
go env -u <VAR>
Remove a persisted setting
go env -json
Environment as JSON
GOFLAGS=<flags>
Default flags for every go command
GOPRIVATE=<glob>
Skip proxy and checksum db for paths
GOPROXY=<url>,direct
Choose the module proxy
GOTOOLCHAIN=<version>
Pick or auto-fetch a toolchain
GOBIN=<dir>
Where go install puts binaries
go tool
List the bundled tools

Recipes

Reproducible small release binary
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \  go build -trimpath -ldflags="-s -w -X main.version=1.2.3" \  -o dist/app .
Coverage report of the whole module
go test -coverprofile=cover.out ./...go tool cover -html=cover.out
Start a module and add a dependency
go mod init example.com/appgo get github.com/google/uuid@latestgo mod tidy
Debug a flaky test
go test -race -count=50 -run '^TestFoo$' ./pkg/...
Develop two local modules together
go work init ./app ./libgo run ./app