TExcelTAD
Home › Go Modules in Depth › Why Go Modules?

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.

bash · try it yourself
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.mod
go: 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:

  1. go mod init wrote a go.mod with the module path and the Go version.
  2. go run refused to guess: since Go 1.16 build commands don't add requirements by themselves. The error tells you the fix.
  3. go mod tidy looked for a module that provides rsc.io/quote, picked its latest version (v1.5.2), and downloaded the dependencies of that module too.
  4. The new go.mod lists 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's go.mod ("module graph pruning").

Note: The go: downloading lines 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
bash · try it yourself
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/@v
file:///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?

  1. A single package
  2. A module: a tree of packages released together
  3. A Git repository
  4. A single binary

In Go 1.27, what does go run . do when main.go imports a package no required module provides?

  1. Downloads the latest version and edits go.mod
  2. Fails and suggests go get; go get or go mod tidy must add the requirement
  3. Uses GOPATH
  4. Vendors the package

Why are indirect dependencies listed in go.mod since Go 1.17?

  1. For licensing
  2. So the go command can load the module graph without reading every dependency's go.mod (graph pruning)
  3. They are needed by gofmt
  4. To lock them like a lockfile for npm