TExcelTAD
Home › Go Modules in Depth › go get & go mod tidy

go get & go mod tidy

Two commands change your requirements. go get changes them on purpose: add this module, move that one to a version, remove another. go mod tidy makes go.mod and go.sum match the code: it adds what your imports need and removes what nothing uses. Most days you edit code and run tidy; you reach for go get when you want a specific version.

go get: version queries

go get path@query accepts more than exact versions:

Query Meaning
@v1.5.1 exactly that version (upgrade or downgrade)
@latest (the default) the highest release version, ignoring pre-releases and retracted versions
@upgrade like @latest, but never downgrades (keeps a newer pre-release you already have)
@patch the latest patch of the current minor version (v1.5.x)
@v1.5 the latest v1.5.x (a version prefix)
@<commit or branch> resolved to a pseudo-version such as v0.0.0-20170915032832-14c0d48ead0c (needs the VCS or a proxy that knows it)
@none remove the requirement, and downgrade or remove whatever needs it

Flags: -u also upgrades the dependencies of the named packages, -u=patch only to newer patches, -t includes test dependencies, and -tool adds a tool (go install & the tool Directive).

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

import (
	"fmt"

	"rsc.io/quote"
)

func main() { fmt.Println(quote.Opt()) }
SRC
go get rsc.io/quote@v1.5.1
go run .
go get rsc.io/quote@patch
go list -m all
go get -u=patch ./...
go get -u ./...
go list -m all
go: downloading rsc.io/quote v1.5.1
go: downloading rsc.io/sampler v1.3.0
go: downloading golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c
go: added golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c
go: added rsc.io/quote v1.5.1
go: added rsc.io/sampler v1.3.0
If a program is too slow, it must have a loop.
go: downloading rsc.io/quote v1.5.2
go: upgraded rsc.io/quote v1.5.1 => v1.5.2
example.com/app
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c
rsc.io/quote v1.5.2
rsc.io/sampler v1.3.0
go: downloading rsc.io/sampler v1.3.1
go: upgraded rsc.io/sampler v1.3.0 => v1.3.1
go: downloading rsc.io/sampler v1.99.99
go: downloading golang.org/x/text v0.43.0
go: upgraded golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c => v0.43.0
go: upgraded rsc.io/sampler v1.3.1 => v1.99.99
example.com/app
golang.org/x/mod v0.41.0
golang.org/x/sync v0.24.0
golang.org/x/text v0.43.0
golang.org/x/tools v0.51.0
rsc.io/quote v1.5.2
rsc.io/sampler v1.99.99

Every change is reported as added, upgraded, downgraded or removed. -u=patch moved sampler from v1.3.0 to v1.3.1; -u then took everything to the latest release. Two things to notice. The new golang.org/x/text v0.43.0 requires x/tools, x/mod and x/sync in its own go.mod, so they joined the module graph although none of their packages is compiled: upgrades pull in requirements, not just code. And sampler went to v1.99.99, a version made to show that "latest" can break you:

bash · try it yourself
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.Hello()) }\n' > main.go
go get rsc.io/quote@v1.5.2 2>/dev/null
go run .
go get -u ./... 2>/dev/null
go run .
go get rsc.io/sampler@v1.3.1
go run .
Ahoy, world!
99 bottles of beer on the wall, 99 bottles of beer, ...
go: downloading rsc.io/sampler v1.3.1
go: downgraded rsc.io/sampler v1.99.99 => v1.3.1
Ahoy, world!

Note: rsc.io/sampler picks its greeting from $LANG, and the container's C.UTF-8 happens to match its pirate entry, hence "Ahoy". The point is the change in behaviour: go get -u is an upgrade of everything, so run your tests after it, and prefer go get -u=patch or upgrading one module at a time.

go mod tidy

tidy loads every package in the module, for all platforms and build tags, including tests, and then:

  • adds requirements for imports nothing provides yet (at the latest version);
  • removes requirements no package needs;
  • moves requirements between the direct and // indirect blocks;
  • adds missing go.sum lines and removes unneeded ones.
bash · try it yourself
mkdir app && cd app
go mod init example.com/app 2>/dev/null
cat > main.go <<'SRC'
package main

import (
	"fmt"

	"github.com/google/uuid"
)

func main() { fmt.Println(uuid.NewMD5(uuid.NameSpaceURL, []byte("tidy")).Version()) }
SRC
go get github.com/google/uuid@v1.6.0 github.com/spf13/pflag@v1.0.10
cat go.mod
go mod tidy -diff; echo "tidy -diff exit=$?"
go mod tidy
go mod tidy -diff; echo "tidy -diff exit=$?"
cat go.mod
go: downloading github.com/spf13/pflag v1.0.10
go: downloading github.com/google/uuid v1.6.0
go: added github.com/google/uuid v1.6.0
go: added github.com/spf13/pflag v1.0.10
module example.com/app

go 1.27.3

require (
	github.com/google/uuid v1.6.0 // indirect
	github.com/spf13/pflag v1.0.10 // indirect
)
diff current/go.mod tidy/go.mod
--- current/go.mod
+++ tidy/go.mod
@@ -2,7 +2,4 @@

 go 1.27.3

-require (
-	github.com/google/uuid v1.6.0 // indirect
-	github.com/spf13/pflag v1.0.10 // indirect
-)
+require github.com/google/uuid v1.6.0

diff current/go.sum tidy/go.sum
--- current/go.sum
+++ tidy/go.sum
@@ -1,4 +1,2 @@
 github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
 github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
-github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk=
-github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=

tidy -diff exit=1
tidy -diff exit=0
module example.com/app

go 1.27.3

require github.com/google/uuid v1.6.0

go get adds what you ask for, even modules nothing imports, and marks them // indirect because it doesn't look at your imports. tidy corrects both. go mod tidy -diff (Go 1.23+) changes nothing: it prints the edits tidy would make and exits with status 1 if there are any, which is the CI check you want ("is go.mod tidy?").

go mod download

go mod download fills the module cache without building: with no arguments, every module in the build list. Container builds use it as a separate, cacheable layer:

dockerfile
COPY go.mod go.sum ./
RUN go mod download          # cached until go.mod/go.sum change
COPY . .
RUN go build -o /out/app .

Test yourself

Which command removes requirements that no package imports any more?

  1. go get -u
  2. go mod tidy
  3. go mod download
  4. go mod verify

What does go get rsc.io/quote@patch do?

  1. Applies a patch file
  2. Moves quote to the latest patch release of its current minor version
  3. Upgrades to the latest major version
  4. Removes quote

What is go mod tidy -diff for?

  1. Showing changes between two versions of a dependency
  2. Failing a CI job when go.mod/go.sum are not tidy, without changing them
  3. Diffing the vendor directory
  4. Comparing two go.work files