go.mod
go.mod is the file at the root of a module that names the module, pins the Go language version, and lists the other modules this code depends on. The toolchain reads it on every build.
Detailed Explanation
A module is a versioned collection of packages. go.mod is the manifest that makes that collection buildable on another machine. Without it, import "example.com/shop/internal/httpapi" has nowhere to resolve. With it, the first line module example.com/shop tells the toolchain that packages under this tree are imported with that prefix.
The file is short on purpose. A typical go.mod has three kinds of line. module sets the import-path prefix. go 1.22 sets the language version the module is written for — new language features and default GODEBUG behaviour follow this number. require lists other modules and the versions this module needs. Indirect requires (dependencies of your dependencies) appear with an // indirect comment after go mod tidy has walked the graph.
You almost never edit versions by hand. go get example.com/other@v1.4.2 adds or upgrades a require. go get example.com/other@none drops it. go mod tidy adds missing requires, removes unused ones, and rewrites go.sum. Treat go.mod and go.sum as a pair: go.sum stores cryptographic hashes so a later download of the same version string cannot silently change.
Replace and exclude are local overrides. replace example.com/other => ../other points an import at a directory on disk, which is how you develop two modules side by side. exclude refuses a specific broken version. Replaces are for your machine or your monorepo; publishing a module whose go.mod is full of replace lines will surprise downstream users, because those replaces apply to their builds too when they are in the same main module.
The Go version line is a compatibility switch. Raising go 1.21 to go 1.22 can change loop-variable scoping and default GODEBUG settings, even if you used no new syntax. Lowering it may make the module unbuildable if the source uses newer features. CI should build with the version in go.mod, not an older toolchain that happens to be installed.
What it costs. A tidy go.mod makes builds reproducible. An untidy one — extra unused requires, missing hashes, a replace left in after a local experiment — makes go test ./... fail on the next laptop. The file is not a lockfile in the npm sense; the selected versions live in go.mod itself, and go.sum only authenticates them.
Code Examples
go.mod — this is not Go source; the toolchain parses it as a module file.
// go.mod — this is not Go source; the toolchain parses it as a module file.
//
// module example.com/shop
//
// go 1.22
//
// require (
// github.com/jackc/pgx/v5 v5.6.0
// golang.org/x/sync v0.8.0
// )# Add or upgrade a dependency to a specific version.
go get github.com/jackc/pgx/v5@v5.6.0
# Recalculate the require list and go.sum from actual imports.
go mod tidy
# Point an import at a sibling directory while you edit both.
# replace example.com/other => ../otherpackage main
import (
"fmt"
"github.com/jackc/pgx/v5/pgxpool"
)
func main() {
// The import path must belong to a module listed in go.mod
// (or to the standard library).
fmt.Printf("%T\n", (*pgxpool.Pool)(nil))
}Common Gotchas
A missing or stale `go.sum` fails the build in CI
`GOSUMDB` and `-mod=readonly` (the default in many pipelines) refuse to fetch a module whose hash is not already recorded. Run `go mod tidy` and commit both files.
Raising the `go` line can change runtime behaviour
language semantics and default `GODEBUG` values follow that version. Read the release notes before bumping it on a library other people import.
`replace` in a published module applies to its importers only when they are building that module as the main module
consumers of your tagged version do not see your local replaces. Do not rely on `replace` as a way to patch a dependency for everyone.
`go.mod` is not JSON or YAML
it is a small custom syntax. Keep it formatted with `go mod edit` or `go mod tidy` rather than hand-aligning columns.