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).
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 allgo: 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:
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/samplerpicks its greeting from$LANG, and the container'sC.UTF-8happens to match its pirate entry, hence "Ahoy". The point is the change in behaviour:go get -uis an upgrade of everything, so run your tests after it, and prefergo get -u=patchor 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
// indirectblocks; - adds missing
go.sumlines and removes unneeded ones.
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.modgo: 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:
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?
- go get -u
- go mod tidy
- go mod download
- go mod verify
What does go get rsc.io/quote@patch do?
- Applies a patch file
- Moves quote to the latest patch release of its current minor version
- Upgrades to the latest major version
- Removes quote
What is go mod tidy -diff for?
- Showing changes between two versions of a dependency
- Failing a CI job when go.mod/go.sum are not tidy, without changing them
- Diffing the vendor directory
- Comparing two go.work files