Go modules look simple on the surface — a go.mod file, a go.sum, and you are done — but the operational choices (vendoring policy, proxy posture, workspace use, replace directives, version pinning) decide whether a build five months from now produces the same binary. This article walks an operator through what each piece does, what to commit, and what to wire up in CI for a real Go service.
How to verify
Confirm the toolchain, inspect the module graph, and check that the proxy and sum database are answering:
go version
go env GOPROXY GOSUMDB GOFLAGS GOMODCACHE
go list -m all | head
go mod graph | head
go mod verify
GOFLAGS=-mod=readonly go build ./...
go mod verify checks every cached module against go.sum. -mod=readonly fails the build if go.mod would be modified — that is the CI guarantee that nobody added a dependency without committing it.
What’s happening
A Go module is a tree of packages versioned together by a go.mod file at its root. The toolchain reads go.mod to find the module path and required dependencies, then resolves the dependency graph using Minimal Version Selection (MVS): pick the highest version that any module in the graph requires, never higher. go.sum records the hash of every module + every go.mod ever loaded, so future builds can detect tampering.
The cache lives in $GOMODCACHE (default $GOPATH/pkg/mod). When a build needs a module that is not cached, the toolchain asks $GOPROXY (default https://proxy.golang.org,direct) for the zip and the go.mod. The transparency layer $GOSUMDB (default sum.golang.org) signs hashes the proxy returns so the proxy itself cannot forge a module. Inside an enterprise that does not allow the public proxy, you set GOPROXY=https://proxy.internal/,direct and GONOSUMCHECK=* or run a mirror like Athens.
Vendoring copies every dependency into vendor/ at the project root. With vendor/ present and consistent, the toolchain builds offline from it and ignores the cache. Workspaces (go.work) glue multiple modules in one repo together for local development — they do not affect builds outside the workspace.
The procedure
-
Initialize the module with a stable path.
go mod init github.com/example/billingThe module path is the canonical import path. Renaming it later is painful — pick the production path on day one.
-
Pin the Go toolchain explicitly. Modern
go.modcarries ago 1.22.5line and an optionaltoolchain go1.22.5. Both should match the version your CI builds with:module github.com/example/billing go 1.22.5 toolchain go1.22.5 require ( github.com/jackc/pgx/v5 v5.6.0 go.opentelemetry.io/otel v1.28.0 ) -
Set CI environment variables. A
.envfile consumed by your pipeline:export GOFLAGS='-mod=readonly -trimpath' export GOPROXY=https://proxy.internal/,https://proxy.golang.org,direct export GOSUMDB=sum.golang.org export GOMODCACHE=/srv/cache/go-mod export CGO_ENABLED=0-trimpathstrips your build host’s filesystem prefix from the binary — required for reproducibility. -
Decide on vendoring. Two camps. Commit
vendor/if you need offline builds, deterministic CI without proxy access, or supply-chain review at PR time. Skip it if you trust the proxy and want a smaller repo. To vendor:go mod vendor git add vendor go.mod go.sumThen build with
GOFLAGS=-mod=vendor. The toolchain will refuse ifvendor/andgo.moddisagree. -
Use workspaces for multi-module repos. A monorepo with
service-a/,service-b/, and a sharedpkg/auth/module:go work init ./service-a ./service-b ./pkg/auth git add go.workCommit
go.workif every developer wants the same view;.gitignoreit if individual developers maintain their own workspace overlays. We commit it. -
Replace directives sparingly. A
replaceline forces the build to use a specific path or version instead of whatrequirelists:replace github.com/upstream/lib => github.com/example/lib-fork v0.0.0-20260601000000-abc1234Use them for emergency forks only. A
replacemakes your module non-importable as a dependency, because consumers see the upstream version but get the fork — undocumented behavior. -
Tighten supply-chain checks. A separate CI job runs:
go mod tidy -diff # fail if go.mod/go.sum would change govulncheck ./... # govulndb advisory check go list -json -m all | nancy sleuth # OSS Index advisory checkgo mod tidy -diffis the linter — block PRs that add unused deps or skip atidy.
Operational notes
go.sumis not optional. Removing it skips the integrity check. Ifgo.sumis missing on a fresh clone, the toolchain refuses to build inreadonlymode.GOPROXY=offdisables network fetches entirely. Useful for air-gapped CI — but every required module must already live invendor/or the cache.- The proxy caches forever; tags do not. If you force-push a tag (don’t), the proxy will keep serving the original commit hash until you rotate the version.
go env -wwrites to~/.config/go/env. Operators sometimes forget that and wonder why CI behaves differently from the laptop. The CI environment must set vars explicitly.- MVS does not bump to the latest patch. You must run
go get -u=patch ./...(patch-only) orgo get -u ./...(minor) to refresh — and re-runtidy.
Stack Harbor runs Go services for clients with reproducible builds wired through a private module proxy, go.work committed in monorepos, and govulncheck blocking on advisories at PR time. See how that fits into our managed operations practice.