Why Go Modules?
Go's dependency system is small on purpose. There is no lockfile, no solver that searches for a combination of versions, no install step that runs scripts and no central registry account. A module declares what it needs in go.mod, the go command picks versions with a short and predictable algorithm, and go.sum records a cryptographic hash of everything it downloaded. This course explains each of those pieces the way a build engineer needs to know them: what the files mean, what the commands change, and why a build picks the versions it picks.
Who this course is for
You can write Go: packages, imports, go run, go test. The lesson Go Packages & Modules in the Go course is enough background; this course starts where it stops. General build ideas (dependency graphs, semver, lockfiles, cross-compilation) are covered in Build Fundamentals, and this course points back to it when Go does something differently.
Packages, modules and repositories
| Unit | What it is | Identified by |
|---|---|---|
| Package | one directory of .go files compiled together |
its import path, e.g. rsc.io/quote |
| Module | a tree of packages released together, with a go.mod at its root |
its module path + a version, e.g. rsc.io/quote v1.5.2 |
| Repository | where the source lives (Git, Mercurial…) | a URL; one repository can hold several modules |
You import packages, but you require and version modules. The go command finds the module that provides a package by matching the longest module path that is a prefix of the import path.
A short history
| Era | How dependencies worked | Problem |
|---|---|---|
GOPATH (2012–2018) |
every import path lived in one shared tree, $GOPATH/src/github.com/…; go get cloned the latest commit |
no versions, two projects couldn't use different versions |
| Vendor tools (dep, glide, govendor) | copy dependencies into vendor/, with tool-specific lock files |
several incompatible tools |
| Modules (Go 1.11, default since 1.16) | go.mod + go.sum, versions from a module proxy, a shared read-only module cache |
this course |
A first session
Every session in this course is a real go run in a container: Go 1.27.3, no network, and a local module proxy directory (GOPROXY=file:///opt/goproxy) that holds a fixed set of public modules. Here is the whole cycle: create a module, import a package nobody has required yet, let go mod tidy find it, and run.
mkdir hello && cd hello
go mod init example.com/hello
cat > main.go <<'SRC'
package main
import (
"fmt"
"rsc.io/quote"
)
func main() {
fmt.Println(quote.Go())
}
SRC
go run .
go mod tidy
go run .
cat go.modgo: creating new go.mod: module example.com/hello
main.go:6:2: no required module provides package rsc.io/quote; to add it:
go get rsc.io/quote
go: finding module for package rsc.io/quote
go: downloading rsc.io/quote v1.5.2
go: found rsc.io/quote in rsc.io/quote v1.5.2
go: downloading rsc.io/sampler v1.3.0
go: downloading golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c
Don't communicate by sharing memory, share memory by communicating.
module example.com/hello
go 1.27.3
require rsc.io/quote v1.5.2
require (
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c // indirect
rsc.io/sampler v1.3.0 // indirect
)
Read it top to bottom:
go mod initwrote ago.modwith the module path and the Go version.go runrefused to guess: since Go 1.16 build commands don't add requirements by themselves. The error tells you the fix.go mod tidylooked for a module that providesrsc.io/quote, picked its latest version (v1.5.2), and downloaded the dependencies of that module too.- The new
go.modlists the direct requirement and, in a second block, the indirect ones: modules your code doesn't import but the build needs. Since Go 1.17 they are written out so that the go command can load the graph without reading every dependency'sgo.mod("module graph pruning").
Note: The
go: downloadinglines go to stderr and appear only when a module isn't in the module cache yet (~/go/pkg/mod). Each session here starts with an empty cache, so you see them every time. On your own machine you will see them once.
Where things live
| Path | What |
|---|---|
go.mod, go.sum |
in your module root; both are committed |
$GOMODCACHE (default $GOPATH/pkg/mod, i.e. ~/go/pkg/mod) |
extracted, read-only module sources, shared by every project |
$GOMODCACHE/cache/download |
the original .mod, .zip and .info files, laid out exactly like a module proxy |
$GOCACHE (~/.cache/go-build) |
the build cache: compiled packages keyed by content hashes |
$GOPATH/bin (~/go/bin) |
binaries from go install |
mkdir demo && cd demo
go mod init example.com/demo 2>/dev/null
go get rsc.io/quote@v1.5.2 2>/dev/null
go env GOPROXY GOSUMDB GOMODCACHE
ls ~/go/pkg/mod/rsc.io
ls ~/go/pkg/mod/cache/download/rsc.io/quote/@vfile:///opt/goproxy
off
/home/learner/go/pkg/mod
quote@v1.5.2
sampler@v1.3.0
list
v1.5.2.info
v1.5.2.lock
v1.5.2.mod
v1.5.2.zip
v1.5.2.ziphash
The cache/download tree has the same layout as a module proxy, which is why a directory can serve as GOPROXY (the image's /opt/goproxy is exactly such a copy). You'll build your own in Upgrades, exclude & retract.
What this course covers
| Part | Lessons |
|---|---|
| Files | go.mod Directives, go.sum & the Checksum Database |
| Versions | go get/tidy, inspecting the graph, Minimal Version Selection, retractions, major versions |
| Local work | replace, workspaces, private modules |
| Building | build tags, cross builds and cgo, version stamping |
| Shipping | vendoring, tools, govulncheck, then a Toolkit, Field Notes, exercises and a mini project |
Test yourself
What does a go.mod file version?
- A single package
- A module: a tree of packages released together
- A Git repository
- A single binary
In Go 1.27, what does go run . do when main.go imports a package no required module provides?
- Downloads the latest version and edits go.mod
- Fails and suggests
go get;go getorgo mod tidymust add the requirement - Uses GOPATH
- Vendors the package
Why are indirect dependencies listed in go.mod since Go 1.17?
- For licensing
- So the go command can load the module graph without reading every dependency's go.mod (graph pruning)
- They are needed by gofmt
- To lock them like a lockfile for npm