Field Notes: Modules in Real Repos
The module system is small, but teams still find ways to hurt themselves with it. These notes are the habits that prevent most of the pain, each with a session that shows the failure.
1. Commit go.sum, always
go.sum is not a cache. Without it, a fresh clone can't verify anything and the go command refuses to build:
mkdir repo && cd repo && git init -q
go mod init example.com/app 2>/dev/null
printf 'package main\n\nimport (\n\t"fmt"\n\n\t"github.com/google/uuid"\n)\n\nfunc main() { fmt.Println(uuid.NewSHA1(uuid.NameSpaceDNS, []byte("x")).Version()) }\n' > main.go
go mod tidy 2>/dev/null
echo "go.sum" > .gitignore # the mistake
git add . && git commit -qm "app"
cd .. && git clone -q repo clone && cd clone
GOFLAGS=-mod=readonly go build . ; echo "exit=$?"main.go:6:2: missing go.sum entry for module providing package github.com/google/uuid (imported by example.com/app); to add:
go get example.com/app
exit=1
Commit go.mod and go.sum together, in the same commit as the code change that needed them. Don't resolve merge conflicts in go.sum by hand: take either side, then run go mod tidy, which rewrites it correctly. Lines for versions you no longer use are harmless and tidy removes them.
2. Let the go command write go.mod
Hand-editing require lines skips everything go get does: it doesn't download the module, check the hash, update go.sum, raise other requirements MVS needs, or keep the indirect block right. The Toolkit session showed the result: the next build stops with "updates to go.mod needed". Use:
| Want | Command |
|---|---|
| add or change a dependency | go get path@version |
| remove one | delete the imports, then go mod tidy (or go get path@none) |
| raise the Go version | go get go@1.27 |
scripted edits that go get can't do (replace, exclude, retract, module path) |
go mod edit -flag |
In CI, two checks keep everybody honest: go mod tidy -diff (fails if go.mod/go.sum aren't tidy) and builds with the default -mod=readonly (never set GOFLAGS=-mod=mod in CI: it lets the build silently change go.mod).
3. replace does not travel
A replace in a library's go.mod works perfectly in the library's own tests, and is ignored by every application that depends on it. The library below pins sampler to v1.99.99 with a replace; its own test shows v1.99.99, but an app using the published library gets the version MVS selects:
# gopublish DIR MODULE VERSION PROXYDIR: write one module version in GOPROXY layout
mkdir -p ~/tools/gopublish && cat > ~/tools/gopublish/main.go <<'SRC'
package main
import (
"archive/zip"
"os"
"path/filepath"
)
func main() {
dir, mod, ver := os.Args[1], os.Args[2], os.Args[3]
at := filepath.Join(os.Args[4], mod, "@v")
os.MkdirAll(at, 0o755)
gomod, _ := os.ReadFile(filepath.Join(dir, "go.mod"))
os.WriteFile(filepath.Join(at, ver+".mod"), gomod, 0o644)
info := `{"Version":"` + ver + `","Time":"2026-01-15T10:00:00Z"}`
os.WriteFile(filepath.Join(at, ver+".info"), []byte(info), 0o644)
f, _ := os.Create(filepath.Join(at, ver+".zip"))
z := zip.NewWriter(f)
filepath.WalkDir(dir, func(p string, d os.DirEntry, err error) error {
if !d.IsDir() {
rel, _ := filepath.Rel(dir, p)
w, _ := z.Create(mod + "@" + ver + "/" + filepath.ToSlash(rel))
data, _ := os.ReadFile(p)
w.Write(data)
}
return nil
})
z.Close()
f.Close()
list, _ := os.OpenFile(filepath.Join(at, "list"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
list.WriteString(ver + "\n")
list.Close()
}
SRC
(cd ~/tools/gopublish && go mod init gopublish 2>/dev/null && go build -o ~/go/bin/gopublish .)
export GOPROXY=file://$HOME/proxy,file:///opt/goproxy
mkdir -p lib && cd lib
go mod init example.com/lib 2>/dev/null
printf 'package lib\n\nimport "rsc.io/sampler"\n\nfunc Greeting() string { return sampler.Hello() }\n' > lib.go
printf 'package lib\n\nimport "testing"\n\nfunc TestGreeting(t *testing.T) { t.Log(Greeting()) }\n' > lib_test.go
go get rsc.io/sampler@v1.3.0 2>/dev/null
go mod edit -replace rsc.io/sampler=rsc.io/sampler@v1.99.99
go mod tidy 2>/dev/null
echo "--- inside the library"
go test -v . | grep lib_test
cd .. && gopublish lib example.com/lib v1.0.0 proxy
mkdir app && cd app
go mod init example.com/app 2>/dev/null
printf 'package main\n\nimport (\n\t"fmt"\n\n\t"example.com/lib"\n)\n\nfunc main() { fmt.Println(lib.Greeting()) }\n' > main.go
go mod tidy 2>/dev/null
echo "--- inside an app that uses lib v1.0.0"
go run .
go list -m rsc.io/sampler--- inside the library
lib_test.go:5: 99 bottles of beer on the wall, 99 bottles of beer, ...
--- inside an app that uses lib v1.0.0
Ahoy, world!
rsc.io/sampler v1.3.0
If a library needs a specific version of a dependency, say so with a require (a minimum that MVS honours everywhere). If it needs a patched dependency, publish the patch (or get it upstream). replace is for the main module: applications, local experiments and workspaces.
4. Major versions are a product decision
- Don't release v2 to clean up names. Each major version splits your users and doubles your maintenance; prefer additions and deprecations (Releasing a v2).
- When you must, change the module path and all internal imports in one commit, tag, and add a
// Deprecated:comment to the last v1 release. - Stay on v0 until the API has real users and you're ready to promise compatibility. Tagging v1.0.0 early and then needing v2 a month later is the common mistake.
- As a consumer, upgrade majors deliberately, one module at a time, with both majors allowed to coexist during the migration (Semantic Import Versioning).
5. Make upgrades routine
MVS never upgrades anything by itself. Without a habit, a codebase drifts years behind:
- Run an update bot (Dependabot, Renovate) on
go.mod, grouped weekly, with CI running tests andgovulncheck. go list -m -u allin a scheduled job lists available upgrades;go get -u=patch ./...is low-risk.- Upgrade the Go toolchain on a schedule (each release is supported until two newer major releases exist), and raise the
goline only when you need new features: it's a minimum for everyone who depends on you.
6. Repository layout
- One module per repository is the default and the easiest to release. Add more modules only for different release cadences or heavy optional dependencies.
- In a multi-module repository, tags are prefixed by the module's directory: module
example.com/repo/toolsatv1.2.0is taggedtools/v1.2.0. - Don't commit
go.workunless every module is always released together (Workspaces). - Put commands in
cmd/<name>/, private code ininternal/(the go command forbids imports ofinternal/packages from outside the parent tree, so it's a real API boundary).
Test yourself
A fresh clone fails with missing go.sum entry. What went wrong?
- The proxy is down
- go.sum was not committed
- The go line is too new
- GOFLAGS is wrong
Your library needs at least v1.3.1 of a dependency. How do you express that so it works for your users?
replace dep => dep v1.3.1require dep v1.3.1(viago get dep@v1.3.1)exclude dep v1.3.0- A comment in the README
How is module example.com/repo/tools (in subdirectory tools/) tagged at v1.2.0?
- v1.2.0
- tools/v1.2.0
- tools-v1.2.0
- v1.2.0-tools