Go CLI Cheat Sheet

Core go command workflows for build, run, test, env, fmt, vet, doc, install, list, clean, toolchains, and everyday module-aware development.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all
## Getting Started
Print Go version
go version

# Show the selected Go toolchain version.

Print Go environment
go env

# Inspect Go environment variables and effective values.

Print Go environment as JSON
go env -json

# Useful for scripting and debugging setup.

Show help
go help

# Show top-level go command help.

Help for build
go help build

# Show detailed help for go build flags and behavior.

## Run Build Install
Run program
go run .

# Compile and run the current module entrypoint.

Run single file
go run main.go

# Compile and run a single Go source file.

Build current package
go build .

# Compile the current package or module target.

Build named binary
go build -o ./bin/app .

# Write the compiled binary to a specific path.

Install package
go install ./cmd/myapp

# Compile and install a package binary.

Install versioned tool
go install golang.org/x/tools/cmd/stringer@latest

# Install a tool at an explicit module version.

## Formatting and Quality
Format package sources
go fmt ./...

# Run gofmt across package sources.

Run vet
go vet ./...

# Report likely mistakes in Go packages.

Run short tests
go test -short ./...

# Run tests while skipping long-running cases.

Run race detector
go test -race ./...

# Detect race conditions in tests or packages.

Run coverage
go test -cover ./...

# Collect statement coverage while testing.

## Documentation and Listing
Show package docs
go doc net/http

# Print docs for a package.

Show symbol docs
go doc net/http Client

# Print docs for a symbol in a package.

List packages
go list ./...

# List packages under the current module.

List package metadata JSON
go list -json ./...

# Emit package metadata for scripts and tooling.

Clean build/test caches
go clean -cache -testcache -modcache

# Remove build outputs and cached files.

## Toolchains and Env
Persist Go env value
go env -w GOPRIVATE=github.com/myorg/*

# Write a Go env default into the user config.

Unset persisted env value
go env -u GOPRIVATE

# Remove a persisted Go env setting.

Force local toolchain
GOTOOLCHAIN=local go test ./...

# Run with the bundled/local toolchain only.

Allow auto toolchain selection
GOTOOLCHAIN=auto go version

# Let the go command choose a newer toolchain if needed.

Show V8 options through go tool
go tool compile -help

# Pass through to tool-specific options when needed.

Recommended next

No recommendations yet.