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
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
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