As a quick reference, to upgrade the Go modules in your project, you can use the following commands:
- Use
go get -u ./...
if you want to update all dependencies to the latest version in the current directory and its subdirectories. - Use
go get -t -u ./...
to update all dependencies in the current directory and its subdirectories including test dependencies. - Use
go get -u all
if you want to update all packages in the main module and all its dependencies including test dependencies. - Use
go get -u
to update all dependencies in the current directory only. - After running the
go get
command, it is a good practice to also rungo mod tidy
. This command ensures that thego.mod
file is in sync with your source code and cleans it up by removing any unused or unnecessary dependencies that may have been created after updating packages to new version.
Disclaimer: Although a Go module is not exactly the same as a package, for the purposes of this article we use the terms module, package and dependency interchangeably, always referring to a Go module as a collection of packages with
go.mod
andgo.sum
files.
Update all Go packages in your project at once to the latest version
Updating external packages is an important part of maintaining any application. New versions often provide bug fixes, remove security vulnerabilities, improve performance and introduce new features. However, in a project that imports many external packages, updating each package one by one can be cumbersome. In such cases, there are great commands that allow you to update all the dependencies in your project at once.
In Go, to do an upgrade of all packages imported by Go files in the given root directory and all its subdirectories, use the following go get
command:
The go get
is a built-in Go command used to download and maintaing packages and dependencies in the project. If you want to know more about this command check out our blog post on What is ‘go get’ command in Go or the official documentation.
The -u
flag instructs the command to update the named packages and their dependencies to the latest available version.
The path ./...
is a special pattern meaning that go get -u
will be executed on the current directory as well as recursively on all its subdirectories. So as a result, by executing this command in the root of the project, all dependencies will be updated.
Whenever you run the
go get
command, it is good practice to also execute:go mod tidyThis command ensures that thego.mod
file matches the source code and cleans thego.mod
andgo.sum
of unused and unnecessary dependencies created when packages are upgraded to new versions.
The regular go get -u ./...
command skips package updates in the *_test.go
test files. But you can update dependencies in the test files as well by using the extra -t
flag:
There is also an alternative version of the command to update all packages in the project at once along with test dependencies:
This command has the same effect as the one above, but its benefit is that you do not have to call it from the project’s root directory to update all dependencies of the project. The name all
means: update all packages in the main module including their dependencies and dependencies of their tests.
Upgrade all packages to the new patch version only
To update all Go modules only to the latest patch version, use the -u=patch
flag in the go get
command:
Update all dependencies only in the current directory
If you need to update Go packages only in the current directory, just use the command:
go get
, the command works only in the current directory.List packages to update in your project
If you want to display all packages in your project together with information if they can be updated, use the command:
github.com/go-kit/log v0.1.0 [v0.2.1]
github.com/go-stack/stack v1.8.0 [v1.8.1]
github.com/google/uuid v1.3.0
As you can see in the output, the package name is followed by the current version of the package in the project, and the latest available version is placed in square brackets. This command returns all the packages in the project, so if any are up to date then the version in square brackets will not be displayed, as in the last line of the sample output.
To display only the direct packages (those that are directly imported by go.mod
) that need updating, you should use the -f
flag and prepare your own output formatting.
go list -u -f '{{if (and (not (or .Main .Indirect)) .Update)}}{{.Path}}: {{.Version}} -> {{.Update.Version}}{{end}}' -m all
An example output:
github.com/go-kit/log: v0.1.0 -> v0.2.1
github.com/go-stack/stack: v1.8.0 -> v1.8.1
This format indicates that if there is a non-empty Update
structure in the data for direct dependencies, then the string should be displayed in the following format:
<package path>: <current version> -> <version to update>
The go list -m
command has a lot of features so if you want to know them all, check the official documentation.
Update a single package to a specific version
If your goal is to update only a single package you can do it in a variety of ways using the go get -u
command with additional flags and modifiers.
If you want to update a single package to the latest version in Go, use go get -u
with the package name:
To upgrade or downgrade a package to a specific version, use the @version
suffix:
To update a Go module to a specific revision, use the @revision
version query:
To get a specific branch or tag version of the package use the @branch
or @tag
version query:
Upgrade a single package only to a new patch version
As with updating all packages at once, a single package can also only be updated to the latest patch version. To upgrade a single package to the latest patch version, you can use one of two ways. The first, using the -u=patch
flag:
@patch
version query:Both of these commands upgrade the specified package to the latest patch version, which means that any changes to the minor or major versions of the package will be ignored.