go.mod Directives
go.mod is a short, line-oriented file. Each line starts with a directive keyword, and related lines can be grouped in parentheses (require ( … )). Go 1.27 understands ten directives, and go mod edit -json prints exactly that set as fields, which makes it the most reliable list to check against.
The ten directives
| Directive | Example | Meaning |
|---|---|---|
module |
module example.com/shop |
the module path: the import-path prefix of every package in the module. A // Deprecated: … comment above it marks the whole module as deprecated |
go |
go 1.26 |
the minimum Go version the module needs, and the language version its code is compiled with. Since Go 1.21 this is a hard minimum: an older toolchain refuses to build it |
toolchain |
toolchain go1.27.3 |
the toolchain suggested for working in this module (only when it is newer than the go line). Only used in the main module |
godebug |
godebug panicnil=1 |
default GODEBUG settings for binaries built from this main module (Go 1.23+) |
require |
require rsc.io/quote v1.5.2 |
a minimum version of a dependency; // indirect marks modules your packages don't import directly |
exclude |
exclude rsc.io/sampler v1.99.99 |
forbid one version of a module (main module only) |
replace |
replace rsc.io/quote => ../quote |
use other content for a module or one version of it: another version, another path, or a local directory (main module only) |
retract |
retract v1.1.0 // broken |
the author says a published version of this module must not be used |
tool |
tool golang.org/x/tools/cmd/stringer |
a tool package runnable with go tool (Go 1.24+) |
ignore |
ignore ./web/node_modules |
directories the go command ignores when matching package patterns like ./... (Go 1.25+) |
Three rules matter more than the table suggests:
- Only the main module's
exclude,replace,godebugandtoolchaincount. When your module is a dependency of someone else, those lines are ignored. Field Notes shows what that means forreplace. requireis a minimum, not a pin.v1.5.2means "at least v1.5.2"; the build may use a newer one if another module needs it (Minimal Version Selection).- There is no version range syntax. No
^,~or>=: every requirement names one version.
Every directive in one file
This session builds a module, then uses go mod edit flags to add each directive (the same edits you could type by hand), prints the file and asks go mod edit -json to parse it back:
mkdir shop && cd shop
go mod init example.com/shop
cat > main.go <<'SRC'
package main
import (
"fmt"
"github.com/google/go-cmp/cmp"
)
func main() { fmt.Println("equal:", cmp.Equal([]string{"apple"}, []string{"apple"})) }
SRC
mkdir -p web/node_modules/junk && echo 'this is not Go' > web/node_modules/junk/x.go
go get github.com/google/go-cmp@v0.6.0
go mod tidy
go list ./...
go mod edit -ignore=./web/node_modules
go list ./...
go mod edit -go=1.26 -toolchain=go1.27.3 -godebug=panicnil=1 \
-exclude=github.com/google/go-cmp@v0.7.0 -retract=v0.1.0
cat go.mod
go run .go: creating new go.mod: module example.com/shop
go: downloading github.com/google/go-cmp v0.6.0
go: added github.com/google/go-cmp v0.6.0
web/node_modules/junk/x.go:1:1: expected 'package', found this
example.com/shop
module example.com/shop
go 1.26
toolchain go1.27.3
require github.com/google/go-cmp v0.6.0
ignore ./web/node_modules
godebug panicnil=1
exclude github.com/google/go-cmp v0.7.0
retract v0.1.0
equal: true
Notice:
- Before the
ignoreline,./...walked intoweb/node_modulesand tripped over a file that isn't Go; afterwards it is skipped. This is the directive's whole purpose: JavaScript tooling (or generated junk) living inside a Go module. - Lowering
goto 1.26 while the toolchain is 1.27.3 madego mod editkeep atoolchain go1.27.3line, which says "work on this module with 1.27.3 if you can". go mod editonly edits text. It doesn't download, check or tidy anything, which makes it the right tool for scripts and the wrong one for adding real dependencies (usego get).
The JSON view
mkdir m && cd m
cat > go.mod <<'MOD'
// Deprecated: use example.com/shop/v2 instead.
module example.com/shop
go 1.27
require (
github.com/google/uuid v1.6.0
rsc.io/quote v1.5.2 // indirect
)
replace rsc.io/quote => ../quote-fork
retract [v1.0.0, v1.0.3] // published from the wrong branch
tool golang.org/x/tools/cmd/stringer
MOD
go mod edit -json{
"Module": {
"Path": "example.com/shop",
"Deprecated": "use example.com/shop/v2 instead."
},
"Go": "1.27",
"Require": [
{
"Path": "github.com/google/uuid",
"Version": "v1.6.0"
},
{
"Path": "rsc.io/quote",
"Version": "v1.5.2",
"Indirect": true
}
],
"Replace": [
{
"Old": {
"Path": "rsc.io/quote"
},
"New": {
"Path": "../quote-fork"
}
}
],
"Retract": [
{
"Low": "v1.0.0",
"High": "v1.0.3",
"Rationale": "published from the wrong branch"
}
],
"Tool": [
{
"Path": "golang.org/x/tools/cmd/stringer"
}
]
}
Comments carry meaning in two places: // Deprecated: before module, and the rationale comment after retract, which go get prints to users of a retracted version. A retract can name a closed interval [low, high].
The go line and toolchains
The go line is also a gate. If a dependency says go 1.28 and you run Go 1.27, the go command wants a newer toolchain. With the default GOTOOLCHAIN=auto it downloads one (as the module golang.org/toolchain) and re-runs itself; in this course's container GOTOOLCHAIN=local forbids that, and the proxy has no toolchains anyway:
mkdir g && cd g
go mod init example.com/g 2>/dev/null
go get go@1.28
go get go@1.27.0
cat go.mod
go env GOTOOLCHAINgo: go@1.28: module golang.org/toolchain: reading file:///opt/goproxy/golang.org/toolchain/@v/list: no such file or directory
go: downgraded go 1.27.3 => 1.27.0
module example.com/g
go 1.27.0
local
go get go@version is the supported way to raise (or lower) the go line: it also updates requirements whose own go lines are affected. In CI, set GOTOOLCHAIN=local when the image's Go version must be the one that builds, so a dependency can't silently pull in a different compiler.
Test yourself
What does require rsc.io/quote v1.5.2 mean?
- Exactly v1.5.2, like a lockfile pin
- At least v1.5.2; the build may select a newer version that another module requires
- Any v1 version
- The latest version, cached as v1.5.2
Which directives are ignored when your module is used as a dependency?
- require and retract
- replace, exclude, toolchain and godebug
- module and go
- None, they all apply
What is the ignore directive for?
- Ignoring a dependency's vulnerabilities
- Excluding directories (e.g. node_modules) from package patterns like ./...
- Skipping go.sum checks
- Ignoring build tags