Command Cheat Sheet

Updated

September 13, 2026

Command Cheat Sheet

Commands this book actually uses. Run them from a module directory unless a filename is listed. Substitute example.com/desk for your module path.

Everyday

Command What it does
go run . Compile the package in this directory and run it
go run file.go Compile and run listed files (no go.mod needed for a toy)
go build -o desk . Write a binary named desk
go test ./... Test every package under this directory
go test -v ./... Same, with names of tests and examples
gofmt -w . Rewrite Go files in place
go fmt ./... Same idea, package-oriented
go vet ./... Compiler-adjacent static checks
go doc ./ticket Print package docs
go doc ./ticket.Open Print one symbol
go env GOOS GOARCH Print this machine’s target defaults
go version Print the toolchain version (this book: 1.27)

Modules

Command What it does
go mod init example.com/desk Create go.mod
go mod tidy Add missing modules, drop unused ones
go get example.com/mod@v1.2.3 Add or bump a requirement
go list -m all Print the module graph

Tests, benches, profiles

Command What it does
go test -cover ./... Statement coverage
go test -race ./... Race detector
go test -bench=. -benchmem ./join Benchmarks plus allocs
go test -bench=BenchmarkJoinPlus -cpuprofile=cpu.pprof ./join CPU profile
go test -bench=BenchmarkJoinPlus -memprofile=mem.pprof ./join Memory profile
go tool pprof -top cpu.pprof Text summary of a profile

Ship

Command What it does
CGO_ENABLED=0 go build -o desk . Pure-Go binary, no cgo
go build -ldflags="-X main.version=1.2.3" -o desk . Stamp var version in package main
GOOS=linux GOARCH=amd64 go build -o desk . Cross-compile
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -ldflags="-X main.version=1.2.3" -o desk . Boring release line
go generate . Run //go:generate lines
go run golang.org/x/vuln/cmd/govulncheck@latest ./... Vulnerability scan
go tool dist list All GOOS/GOARCH pairs

Optional extra

Command What it does
staticcheck ./... Third-party static analysis (install separately)

Standard library packages (quick reference)

Package Since Key items
slices 1.21 Sort, SortFunc, Contains, BinarySearch, Compact, Reverse, Clone, Collect
maps 1.21 Clone, Copy, Keys, Values, Equal, DeleteFunc, Collect
cmp 1.21 Compare, Ordered constraint
iter 1.23 Seq[V], Seq2[K,V] — push-iterator types for range-over-function
log/slog 1.21 NewTextHandler, NewJSONHandler, HandlerOptions, With, SetDefault
sync Mutex, RWMutex, WaitGroup (.Go since 1.25), Once, Map
sync/atomic Int64, Bool, Pointer[T] — typed atomics
context Background, WithCancel, WithTimeout, WithCancelCause (1.20), Cause (1.20)
testing T.Run, T.Helper, T.TempDir, T.Setenv, T.Context (1.21), B.Loop (1.24)
net/http ServeMux with method patterns (1.22), PathValue (1.22)

Reminder 1: go run is a compile

Save as hello.go:

// hello.go
package main

import "fmt"

func main() {
    fmt.Println("desk is open")
}

Run:

go run hello.go

Output:

desk is open

There is no interpreter. go build -o hello hello.go then ./hello prints the same line.

Reminder 2: go test runs examples

Save as go.mod:

module example.com/desk

go 1.27

Save as greet.go:

// greet.go
package desk

func Greet() string {
    return "desk is open"
}

Save as greet_test.go:

// greet_test.go
package desk

import "fmt"

func ExampleGreet() {
    fmt.Println(Greet())
    // Output:
    // desk is open
}

Run:

go test -v .

Output:

=== RUN   ExampleGreet
--- PASS: ExampleGreet (0.00s)
PASS
ok      example.com/desk    0.002s

If the printed line drifts from // Output:, the test fails. That is documentation you cannot ignore.

The boring rule

  • fmt / vet / test on every change.
  • govulncheck and a stamped CGO_ENABLED=0 build before you ship.
  • When in doubt, go doc cmd/go is the long form of this page.