Go study
Offline — showing the cached copy.
Home / Go / 2. Toolchain, modules, workspaces

2. Toolchain, modules, workspaces

One-liner. go.mod is a contract about language version and dependencies; the toolchain line decides who compiles it.

Memorize

  • go.mod: module path, go directive (language semantics + minimum toolchain), requirements.
  • go.sum pins content hashes; the checksum database (sum.golang.org) detects tampering.
  • Semantic import versioning: major ≥ 2 requires /v2 in the module path.
  • toolchain directive (1.21): if the installed toolchain is older, Go downloads the required one.
  • GOTOOLCHAIN=local pins to the installed version (air-gapped CI, reproducible builds).
  • Workspaces (go.work): build several modules together locally; don't commit for libraries.
  • tool directive (1.24): go get -tool records dev tools in go.mod, replacing tools.go.
  • go mod tidy is not optional hygiene — it prunes the graph that go.sum covers.
BUILD RESOLUTION go.mod requirements module cache toolchaindirective compile go.sumverification
Build resolution

Trap. Committing go.work — it silently overrides dependency resolution for everyone who clones the repo. Do this. Bump the go directive deliberately, in its own commit, after CI agrees.

Check

A repo declares toolchain go1.24.1; CI has Go 1.23 installed. What happens by default?