go mod tidy: Usage, Options & Common CI Errors
Make go.mod and go.sum match what your code actually imports.
go mod tidy ensures go.mod lists every module the build needs and nothing it does not, and that go.sum has the necessary checksums. CI commonly verifies the files are already tidy.
What it does
Scans all imports (including tests), adds missing requirements, removes unused ones, and updates go.sum. It can also set the module’s Go version with -go.
Common usage
Terminal
go mod tidy # tidy go.mod and go.sum
go mod tidy -go=1.22 # set the go directive version
# CI gate: fail if tidy would change anything
go mod tidy && git diff --exit-code go.mod go.sumCommon CI error: go.mod is not tidy
A build fails with "missing go.sum entry for module ...; to add it: go mod download" or the CI tidy-check diff is non-empty, because a contributor changed imports without running tidy. Fix: run go mod tidy locally and commit the updated go.mod and go.sum.
Options
| Flag | Effect |
|---|---|
| -go=<version> | Set the go directive |
| -v | Log removed modules |
| -e | Continue past errors |
Related guides
Go "checksum mismatch" - Fix go.sum Verification Errors in CIFix Go "verifying module: checksum mismatch" in CI -- the downloaded module hash does not match go.sum, from…
go mod download: Usage, Options & Common CI Errorsgo mod download fetches module dependencies into the module cache. Learn how it speeds up CI, the -x flag, an…
go mod verify: Usage & Common CI Errorsgo mod verify checks that the modules in your cache match the checksums in go.sum. Learn what it guarantees a…
go build: Usage, Options & Common CI Errorsgo build compiles Go packages and binaries. Learn -o, ./..., cross-compiling with GOOS/GOARCH, and how to fix…