Go cheat sheet

A scannable Go reference: 10 short snippets across 5 topics, each linking back to the lesson it came from.

At a glance

TopicWhat it covers
Goroutines and channelsA goroutine is a function running concurrently, scheduled onto a small pool of operating-system threads. Its initiallesson
Setting up Go: modules, toolchain and editorsGo installs as one toolchain: the go command is the compiler, the build system, the test runner and the module managerlesson
Packages, project layout and dependenciesA package is a directory. Every file in it declares the same package name, and the whole package is compiled as a unitlesson
Testing, benchmarking and fuzzingTable-driven tests, subtests and helpers, benchmarks with allocation counts, fuzzing, httptest, and coverage that meanslesson
Profiling, performance and deploymentpprof for CPU and memory, escape analysis and allocations, the execution tracer, GOMAXPROCS in containers, and shippinglesson

Quick snippets

Goroutines and channels

Coordinating work

ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()

select {
case v := <-ch:
    fmt.Println("got", v)
case <-ctx.Done():
    fmt.Println("timeout:", ctx.Err())
}

// run the race detector before you trust any concurrent code
// go test -race ./...

Full lesson: Goroutines and channels →

Setting up Go: modules, toolchain and editors

Install and inspect the toolchain

go version                     # go1.24.1 darwin/arm64
go env GOROOT GOPATH GOMODCACHE GOTOOLCHAIN

# persist a default for every future shell
go env -w GOPROXY=https://proxy.golang.org,direct
go env -w GOFLAGS=-mod=mod

Modules: go.mod and go.sum

mkdir notes && cd notes
go mod init example.com/notes      # creates go.mod; the path is the import prefix
go get github.com/google/uuid      # adds a require line and updates go.sum
go mod tidy                        # add what is used, remove what is not
go mod graph                       # who depends on what
go mod vendor                      # copy dependencies into vendor/

Modules: go.mod and go.sum

module example.com/notes

go 1.24                // the language version this module is written for
toolchain go1.24.1     // preferred toolchain when building

require (
    github.com/google/uuid v1.6.0
)

exclude github.com/old/broken v1.2.3

replace example.com/legacy => ../legacy   // local override while developing

Full lesson: Setting up Go: modules, toolchain and editors →

Packages, project layout and dependencies

Packages and internal

notes/
  go.mod
  cmd/notes/main.go        package main   -> import path example.com/notes/cmd/notes
  internal/store/store.go  package store  -> importable only inside example.com/notes
  internal/config/config.go
  user.go                  package notes  -> the public surface of the library
  user_test.go

Dependencies and versioning

go get github.com/spf13/[email protected]     # pin an exact version
go get -u=patch ./...                    # apply patch upgrades only
go list -m -u all                        # what could be upgraded
go mod why github.com/some/dep           # why it is required at all
go mod verify                            # check the cache against go.sum
go mod vendor
go build -mod=vendor ./...               # build from vendor/, no network

Full lesson: Packages, project layout and dependencies →

Testing, benchmarking and fuzzing

Benchmarks, fuzzing and coverage

go test ./...
go test -run TestDiscount -v ./pricing
go test -race -count=1 ./...
go test -bench=. -benchmem ./pricing     # ns/op and B/op, allocs/op
go test -fuzz=FuzzParsePort -fuzztime=30s ./pricing
go test -coverprofile=cover.out ./... && go tool cover -html=cover.out

Full lesson: Testing, benchmarking and fuzzing →

Profiling, performance and deployment

Allocations and escape analysis

go build -gcflags='-m' ./...        # report escape analysis decisions
go test -bench=. -benchmem ./...    # ns/op, B/op, allocs/op

Shipping: tracer, containers and cross-compilation

# execution tracer: scheduling, GC, syscalls over a window
go test -trace trace.out ./...
go tool trace trace.out

# one static binary, no interpreter, no shared libraries
CGO_ENABLED=0 GOOS=linux GOARCH=amd64   go build -trimpath -ldflags="-s -w" -o dist/notes-linux-amd64 ./cmd/notes

# stamp the version into the binary
go build -ldflags "-X main.version=$(git describe --tags --always)" ./cmd/notes

Shipping: tracer, containers and cross-compilation

FROM golang:1.24 AS build
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -trimpath -o /out/app ./cmd/notes

FROM gcr.io/distroless/static-debian12
COPY --from=build /out/app /app
ENTRYPOINT ["/app"]

Full lesson: Profiling, performance and deployment →

FAQ

Is this Go cheat sheet free to use?
Yes. No sign-up and no tracking: the page is static, every example is on the page itself, and you can print it or save it as a one-page reference.
Where do the examples come from?
Every snippet is taken from the 5 lessons of the Go course on this site, and each section links back to the lesson it was pulled from.
How do I go deeper than a cheat sheet?
Open the full Go course — it carries the worked explanations, the edge cases and the exercises behind every line here.

Node.js PHP Java HTTP Rust Spring Boot

Last refreshed 2026-09-27.