Skip to content
Latchkey

go mod download: Prefetch Modules in CI

go mod download downloads the modules named in go.mod (or all of them) into the local module cache so later build and test steps run without network round trips.

Prefetching modules in a dedicated, cacheable step makes the build step deterministic and offline. It is the Go equivalent of warming ~/.cargo/registry.

What it does

go mod download populates the module cache ($GOMODCACHE, default ~/go/pkg/mod) with the modules required by go.mod. With no arguments it downloads everything in the build list; -json prints structured records with paths and hashes; -x shows the underlying commands.

Common usage

Terminal
go mod download
go mod download -x                      # show what it fetches
go mod download -json                   # structured output
go mod download all && go build ./...   # warm then build

Flags

FlagWhat it does
(no args)Download all modules in the build list
-jsonEmit JSON records per module (Path, Version, Dir, Sum)
-xPrint the commands being run
GOFLAGS=-mod=readonlyEnv: forbid edits to go.mod/go.sum in later steps
GOPROXYEnv: module proxy URL(s) to download from

In CI

Run go mod download first and cache ~/go/pkg/mod keyed on go.sum, then set GOFLAGS=-mod=readonly for build and test so they fail rather than silently changing go.mod. Set GOPROXY to your proxy and GONOSUMCHECK/GONOSUMDB/GOPRIVATE appropriately for private modules.

Common errors in CI

"missing go.sum entry for module ...; to add it: go mod download ..." means go.sum is incomplete; run tidy/download and commit. "verifying module: checksum mismatch ... SECURITY ERROR" means the downloaded hash differs from go.sum (corruption or a changed upstream). "dial tcp: lookup proxy.golang.org: ... i/o timeout" or "410 Gone" point at proxy/network issues; set GOPROXY or add the private host to GOPRIVATE.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →