The go.mod file declares a module: its path, the Go version it needs and the other modules it requires, each with an exact version. The go.sum file holds cryptographic hashes of those dependencies so the go command can verify that what it downloads is what you expected. You commit both files to version control.
Together, go.mod and go.sum make builds reproducible. This guide reads both files line by line, explains every common directive and shows which parts you edit yourself and which the tools maintain.
A realistic go.mod looks like this.
| Directive | Meaning |
|---|---|
module | The module path; the prefix of all import paths in the module. |
go | The minimum Go language version the module is written for. |
toolchain | A suggested Go toolchain version to use (optional, added by the tools). |
require | A dependency and the minimum version your module needs. |
replace | Substitutes a module with another version or a local folder. |
exclude | Forbids a specific version of a module. |
retract | Marks versions of your own module as not to be used. |
tool | Records a tool dependency such as a code generator (Go 1.24 and later). |
Only module and go appear in every file. The rest show up when you need them.
Versions follow semantic versioning with a leading v: v1.6.0 is major 1, minor 6, patch 0. A requirement is a minimum: Go uses Minimal Version Selection, which picks the highest of the minimum versions required by all modules in the build, not the newest release that exists.
The comment // indirect marks a module your code does not import directly. It is there because a dependency needs it, or because the version had to be pinned. You never type this comment; go mod tidy adds and removes it.
Modules at major version 2 or higher carry the version in the path, for example github.com/example/lib/v2 v2.3.1. Your imports must include /v2 as well.
Each line in go.sum has three fields: module path, version and a hash.
h1: is the hash of the module's full source tree./go.mod is the hash of only that module's own go.mod file, which Go needs to resolve the dependency graph even for modules whose code it never downloads.The hashes use SHA-256 and are checked against a copy kept by the checksum database (sum.golang.org) the first time a module is downloaded. After that, go.sum guards you: if the code for v1.6.0 ever changes upstream, the build fails with a SECURITY ERROR instead of silently using different code.
Treat the two files differently.
go.mod: you may edit it, for example to change the go line or add a replace, but prefer commands such as go get, go mod edit and go mod tidy.go.sum: never edit it. It is generated and verified by the tools. A merge conflict is fixed by running go mod tidy and committing the result.go mod verify re-checks the downloaded copies in the module cache against go.sum and prints all modules verified when they match.
Yes. Without go.sum, a fresh checkout has nothing to verify downloads against, and the first fetch trusts whatever the server returns. Commit both files for applications and libraries. A line in go.sum for a version you no longer use is harmless; go mod tidy removes stale lines.
It lists hashes for every module version in the dependency graph, including indirect ones, and two lines per version (source tree and go.mod).
It sets the language version used to compile the module and, since Go 1.21, a hard minimum: a toolchain older than the line refuses to build or fetches a newer toolchain automatically.
You can, with go mod tidy, but you lose the protection of your existing recorded hashes. Do it only when you intend to re-verify everything.
The module is needed by the build but not imported by your own packages.
go.mod states what your module is and which module versions it needs. go.sum stores hashes that prove downloaded dependencies have not changed. Edit go.mod through the go commands, never touch go.sum by hand, and commit both files so every machine builds the same code.