TExcelTAD
Home › Go Modules in Depth › go.mod Directives

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, godebug and toolchain count. When your module is a dependency of someone else, those lines are ignored. Field Notes shows what that means for replace.
  • require is a minimum, not a pin. v1.5.2 means "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:

bash · try it yourself
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 ignore line, ./... walked into web/node_modules and 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 go to 1.26 while the toolchain is 1.27.3 made go mod edit keep a toolchain go1.27.3 line, which says "work on this module with 1.27.3 if you can".
  • go mod edit only 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 (use go get).

The JSON view

bash · try it yourself
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:

bash · try it yourself
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 GOTOOLCHAIN
go: 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?

  1. Exactly v1.5.2, like a lockfile pin
  2. At least v1.5.2; the build may select a newer version that another module requires
  3. Any v1 version
  4. The latest version, cached as v1.5.2

Which directives are ignored when your module is used as a dependency?

  1. require and retract
  2. replace, exclude, toolchain and godebug
  3. module and go
  4. None, they all apply

What is the ignore directive for?

  1. Ignoring a dependency's vulnerabilities
  2. Excluding directories (e.g. node_modules) from package patterns like ./...
  3. Skipping go.sum checks
  4. Ignoring build tags