Go Reference ยท The go Command and Tools
The go get command
Reference for go get: add or upgrade dependencies, version queries (@latest, @none, @v1.2.3), -u, -t, -tool, downgrades and minimal version selection.
go get changes the dependencies of the module you are working in. It adds a new requirement to go.mod, upgrades or downgrades an existing one, or removes one, and it downloads the code so your next build works. Since Go 1.18 it no longer builds or installs commands: that job belongs to go install.
Each argument has the form module@query. The query says which version you want; without one, go get uses @upgrade, which means the latest release (or keeps what you already have if it is newer).
go get github.com/google/uuid # add, or upgrade to latest go get github.com/google/uuid@v1.6.0 # exact version go get golang.org/x/text@none # remove the requirement go get -u ./... # upgrade dependencies of this module
Try it
Choosing a version is a comparison problem, and Go's rule is simple: among all requirements anywhere in the graph, use the highest minimum. The examples implement the version comparison and that rule with the standard library only.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Syntax
go get [-d] [-t] [-u] [-tool] [-x] [build flags] [packages]
| Argument or flag | Description |
|---|---|
module |
Same as module@upgrade. |
module@latest |
Latest tagged release, or the newest commit if no release exists. Unlike @upgrade, it ignores the version you have now, so it can lower a pre-release to the latest release. |
[email protected] |
Exactly this version. Can be a downgrade. |
module@upgrade |
The latest release unless you already have something newer. |
module@patch |
Latest patch release of the version you currently use. |
module@none |
Remove the requirement. Modules that depended on it may be downgraded too. |
module@main, @commit |
A branch, or a commit hash; recorded as a pseudo-version like v0.0.0-20250102150405-abcdef123456. |
module@>=v1.5 |
A comparison query; the lowest matching version is used. |
-u |
Also update the dependencies of the named packages to their latest minor or patch releases. |
-u=patch |
Update dependencies, but only to patch releases. |
-t |
Also consider modules needed to build the tests of the named packages. |
-tool |
Add a command dependency as a tool directive in go.mod (Go 1.24). |
-x |
Print the commands being run. |
Go version bump: if the new dependency needs a newer Go than your go line, go get raises that line, and may require a newer toolchain. Use go get [email protected] to change the language version yourself.
Errors: module ... found, but does not contain package ... means the version has no package at that path (check the major-version suffix: /v2). unknown revision means the query matched no tag or commit.
Usage notes
- Only changes
go.modandgo.sum. No binary is built and nothing is installed. For tools usego install tool@version, or record them withgo get -tooland run them withgo tool. - Upgrade all dependencies:
go get -u ./...thengo mod tidy. Add-tto include test dependencies. This is a deliberate step; Go never upgrades by itself. - Direct versus indirect. Adding a package you import yields a plain
require; other modules pulled in appear with// indirect. Rungo mod tidyafterwards to settle the marks. - Downgrades cascade. Asking for an older version of
Amay also lower other modules that needAat a higher version, because the build must stay consistent. - Major versions are different import paths.
github.com/x/y/v2andgithub.com/x/yare separate modules. To move from v1 to v2 you change the import path, thengo get github.com/x/y/v2. - Checksums. New modules are verified against
sum.golang.organd recorded ingo.sum. Private code should be listed inGOPRIVATE. - Outside a module
go getdoes nothing useful; rungo mod initfirst. - Obsolete flags.
-d(download only) is now the default behavior and is ignored. Old advice that saysgo get -u toolinstalls a program is wrong for modern Go. - Replace and vendor. With a
vendor/directory, rungo mod vendorafter changing dependencies so the folder matches.
Related
- go mod, go install, go list, go env
- Lessons: Dependencies, Modules