Irbisa · Cheatsheet
Modules, Build & Tooling
go.mod and MVS, build tags, embed, ldflags, linters, static binaries
What are
GOROOTandGOPATH, and why doesGOPATHmatter much less since modules?What is the difference between a package and a module in Go?
What do
go.modandgo.sumeach hold, and which of them decides the dependency versions you build?// go.mod: which versions are builtmodule github.com/acme/billing go 1.25 require (…What does
go mod tidydo, how isgo getdifferent from editinggo.modby hand, and what doesgo mod verifycheck?What does
// indirecton arequireline ingo.modmean, and why does a moderngo.modlist so many of them?// go.mod of a go 1.17+ module: two require blocksmodule github.com/acme/billing go 1.25 require (…How do you install a Go command-line tool such as
mockgentoday, and why doesgo getno longer produce a binary?# wrong since Go 1.18: adds a requirement to go.mod, builds nothing$ go get go.uber.org/mock/mockgen$ mockgen -versionzsh: command not found: mockgen # right: build a pinned version into $GOBIN, ignoring the current go.mod…What does
go generatedo, why doesgo buildnever run it, and should the generated files be committed?package order //go:generate go tool stringer -type=Status type Status int…What do
gofmt,go vet,staticcheckandgolangci-linteach catch, and how do you run them in CI?# .golangci.yml (golangci-lint v2)version: "2" linters: default: standard # errcheck, govet, ineffassign, staticcheck, unused enable:…How do you document Go code, and what do doc comments and
go doclook like?// Package billing charges customers and issues invoices.package billing import "errors" // ErrDeclined is returned when the card is refused.…Given these requirements, which version of
golang.org/x/netgets built, and why does Go need no lock file to keep it that way?// your go.mod (without the // indirect lines tidy would add)require ( github.com/acme/kit v1.3.0 github.com/acme/api v0.9.0) // kit v1.3.0's go.mod: require golang.org/x/net v0.28.0// api v0.9.0's go.mod: require golang.org/x/net v0.31.0 // newest x/net on the proxy today: v0.45.0Why does a v2 module need
/v2in its module path, and what goes wrong if you tagv2.0.0without changing it?// the library's go.mod, from v2 on:// module github.com/acme/lib/v2 package main import (…Your library works around a broken dependency with
replacein itsgo.mod, but services that import the library still get the broken version. Why?How do you make Go fetch private modules from your company Git, and how do
GOPROXY,GOPRIVATEand vendoring fit together?# wrong: a private path goes to the public mirror and checksum DB$ go get git.corp.example/team/lib@v1.2.0verifying module: git.corp.example/team/lib@v1.2.0: reading https://sum.golang.org/lookup/...: 410 Gone # right: one variable covers GONOPROXY and GONOSUMDB for these prefixes$ go env -w GOPRIVATE='git.corp.example/*,github.com/acme/*'…What does the
goline ingo.modcontrol, and how do thetoolchainline andGOTOOLCHAINdecide which Go actually builds the code?// go.modmodule github.com/acme/billing go 1.24.0 // language version + minimum toolchaintoolchain go1.25.3 // preferred toolchain when this is the main module…How do you pin dev tools like
mockgenorstringerfor the whole team: the oldtools.gofile or the Go 1.24tooldirective?# Go 1.24+: record the tool in go.mod$ go get -tool go.uber.org/mock/mockgen@v0.6.0 # go.mod now containstool go.uber.org/mock/mockgen…How do you lay out a Go service repository, and what does the
internaldirectory enforce?github.com/acme/billing/├── go.mod├── cmd/│ ├── api/main.go // one directory per binary: config + wiring only│ └── worker/main.go└── internal/ // importable only from github.com/acme/billing/...…Why does Go forbid import cycles, and how do you break a cycle between two of your packages?
package store import ( "context" "database/sql")…What are build constraints (
//go:build,_linux.gosuffixes), and how do you cross-compile a Go binary for another OS and architecture?// file: internal/store/pg_integration_test.go//go:build integration package store import "testing"…Built with
-ldflags="-X main.version=1.4.0 -X main.commit=abc -X main.buildTime=2026-10-14 -X main.env=prod"and noAPP_ENV. What does it print?package main import ( "fmt" "os") const commit = "none" var version = "dev"var buildTime stringvar env = os.Getenv("APP_ENV") func main() { fmt.Printf("%s %s %s %q\n", version, commit, buildTime, env)}How does
govulncheckdiffer from a version-matching dependency scanner, and what does it check?The same commit builds on every laptop but fails in CI with
no required module provides package, and the repo has a git-ignoredgo.work. What is going on?// go.work at the repo root: git-ignored here, which is the whole buggo 1.25 use ( ./api ./lib…Two CI runs of the same commit produce binaries with different hashes. What makes a Go build reproducible, and how does a binary record how it was built?
# same commit on two machines: the hashes should match$ CGO_ENABLED=0 go build -trimpath -o app ./cmd/app$ sha256sum app # what the binary says about itself$ go version -m app…What is profile-guided optimisation (PGO) in Go, what does it actually optimise, and how do you roll it out for a service?
# 1. CPU profiles from production, under real traffic$ curl -o cpu-1.pprof 'http://api-1:6060/debug/pprof/profile?seconds=30'$ curl -o cpu-2.pprof 'http://api-2:6060/debug/pprof/profile?seconds=30' # 2. merge them so one instance or one spike does not dominate$ go tool pprof -proto cpu-1.pprof cpu-2.pprof > cmd/api/default.pgo…