TExcelTAD
Home › Go Modules in Depth › go install & the tool Directive

go install & the tool Directive

Go projects use Go-written tools: code generators (stringer, mockgen, protoc-gen-go), linters, migration tools. There are two ways to get one, and they answer different questions:

go install pkg@version tool directive (Go 1.24+)
Question "install this program on my machine" "this project uses this tool, at this version"
Version recorded in nowhere (your shell history) your go.mod and go.sum
Run with ~/go/bin/name (on PATH) go tool name
Same version for every developer and CI only if everyone types the same command yes

go install pkg@version

With a version suffix, go install builds the package outside your module: it ignores the current directory's go.mod and uses the tool's own requirements, then puts the binary in $GOBIN (default ~/go/bin).

bash · try it yourself
go install golang.org/x/tools/cmd/digraph@v0.51.0
ls ~/go/bin
go version -m ~/go/bin/digraph | head -3
mkdir app && cd app
go mod init example.com/app 2>/dev/null
printf 'package main\n\nimport (\n\t"fmt"\n\n\t"rsc.io/quote"\n)\n\nfunc main() { fmt.Println(quote.Go()) }\n' > main.go
go mod tidy 2>/dev/null
echo "--- who requires golang.org/x/text?"
go mod graph | digraph preds golang.org/x/text@v0.0.0-20170915032832-14c0d48ead0c
echo "--- a path from quote to it"
go mod graph | digraph somepath rsc.io/quote@v1.5.2 golang.org/x/text@v0.0.0-20170915032832-14c0d48ead0c
go: downloading golang.org/x/tools v0.51.0
digraph
/home/learner/go/bin/digraph: go1.27.3
	path	golang.org/x/tools/cmd/digraph
	mod	golang.org/x/tools	v0.51.0	h1:k4Xc/1Om9jwkBJBo4NVLMSARBoWtK10mx+W5BnXCeAI=
--- who requires golang.org/x/text?
example.com/app
rsc.io/sampler@v1.3.0
--- a path from quote to it
rsc.io/quote@v1.5.2 rsc.io/sampler@v1.3.0
rsc.io/sampler@v1.3.0 golang.org/x/text@v0.0.0-20170915032832-14c0d48ead0c

example.com/app is a predecessor because tidy wrote the indirect requirement into its go.mod; the real reason is the chain somepath prints. digraph reads go mod graph's edge list and answers graph questions (somepath, allpaths, reverse, succs, preds), which is handy when go mod why isn't enough because you want requirement paths rather than imports.

Rules for go install pkg@version: the version is required for packages outside your module (@latest is fine for a quick try), the tool's go.mod must not have directory replace directives, and nothing is written to your go.mod. Without @version, go install ./cmd/app builds a package of the current module with the current module's requirements.

The tool directive

Before Go 1.24, projects pinned tools with a tools.go file of blank imports behind a //go:build tools tag. The tool directive replaces that trick. go get -tool adds the tool's module to the requirements and a tool line; go tool name builds (with caching) and runs it.

bash · try it yourself
mkdir app && cd app
go mod init example.com/app 2>/dev/null
cat > color.go <<'SRC'
package main

//go:generate go tool stringer -type=Color

type Color int

const (
	Red Color = iota
	Green
	Blue
)
SRC
cat > main.go <<'SRC'
package main

import "fmt"

func main() { fmt.Println(Red, Green, Blue, Color(7)) }
SRC
go get golang.org/x/sync@v0.24.0 2>/dev/null
go get -tool golang.org/x/tools/cmd/stringer@v0.51.0
grep -E '^(tool|require)|x/tools' go.mod
go tool | grep stringer
go generate ./...
head -1 color_string.go
go run .
go: downloading golang.org/x/tools v0.51.0
go: downloading golang.org/x/mod v0.41.0
go: added golang.org/x/mod v0.41.0
go: added golang.org/x/tools v0.51.0
require (
	golang.org/x/tools v0.51.0 // indirect
tool golang.org/x/tools/cmd/stringer
stringer (golang.org/x/tools/cmd/stringer)
// Code generated by "stringer -type=Color"; DO NOT EDIT.
Red Green Blue Color(7)

What happened:

  • go.mod gained tool golang.org/x/tools/cmd/stringer and requirements for x/tools and its dependencies. Tool dependencies take part in MVS with your own: they share one module graph. That's convenient (one version of everything) and occasionally annoying (a tool can force a newer version of a library you use).
  • go tool lists the built-in tools (vet, cover, pprof…) plus your module's tools, shown as name (package).
  • //go:generate go tool stringer … makes go generate use exactly the pinned version, on every machine.
  • go get -tool pkg@none removes a tool; go get -u tool upgrades all tools; go install tool installs them all into ~/go/bin.

Note: The go get golang.org/x/sync@v0.24.0 line is specific to this course's offline proxy: x/tools v0.51.0 asks for x/sync v0.23.0, whose code isn't in the proxy, so the session first requires the newer v0.24.0 that is, and MVS selects it. It's also a live example of MVS raising a tool's dependency.

Which one when?

  • Project tools (generators, linters run in CI, migration tools): tool directive. The version is reviewed in pull requests like any dependency.
  • Personal tools you use across projects (gopls, dlv, govulncheck): go install …@version, or your OS package manager.
  • A tool with a heavy or conflicting dependency tree: keep it out of your main module, either with go install in CI or with go tool -modfile=tools/go.mod (a separate module file just for tools; go get -tool -modfile=tools/go.mod … writes to it).

Test yourself

Does go install golang.org/x/tools/cmd/digraph@v0.51.0 change your go.mod?

  1. Yes, it adds a require line
  2. No, with an @version it builds outside the current module
  3. Yes, it adds a tool line
  4. Only in workspaces

What does go get -tool golang.org/x/tools/cmd/stringer@v0.51.0 do?

  1. Installs stringer into ~/go/bin
  2. Adds a tool line and the tool's requirements to go.mod, so go tool stringer runs that version
  3. Downloads stringer's source into vendor/
  4. Runs stringer once

What replaced the old tools.go file with blank imports?

  1. go.work
  2. The tool directive in go.mod (Go 1.24)
  3. GOFLAGS
  4. go install