Cookies management by TermsFeed Cookie Consent

🆕 How to update all Go packages to the latest version

go get go module

As a quick reference, to upgrade the Go modules in your project, you can use the following commands:

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 and go.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:

go get -u ./…

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 tidy
This command ensures that the go.mod file matches the source code and cleans the go.mod and go.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:

go get -t -u ./…

There is also an alternative version of the command to update all packages in the project at once along with test dependencies:

go get -u all

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:

go get -u=patch ./…
or
go get -u=patch all
This command upgrades packages only to the latest patch version as defined in the Semantic Versioning version: MAJOR.MINOR.PATCH. It means that any changes to the minor or major versions of the package will be ignored. This is useful when you want to ensure that your project is up-to-date with the latest security patches and bug fixes, without introducing any breaking changes. By upgrading to the latest patch version regularly, you can ensure that your codebase is secure and reliable, without having to worry about compatibility issues or other problems.

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 -u
By default, if no path is defined in the 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:

go list -m -u all
This command lists all project’s Go modules along with the version that is currently installed and the latest available version. Note that this command prints out direct dependencies, i.e. those that the project directly imports, as well as indirect dependencies, i.e. those that are required by direct dependencies. 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]
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:

go get -u github.com/go-kit/log

To upgrade or downgrade a package to a specific version, use the @version suffix:

go get -u github.com/go-kit/log@v0.2.0

To update a Go module to a specific revision, use the @revision version query:

go get -u github.com/go-kit/log@0b69c70

To get a specific branch or tag version of the package use the @branch or @tag version query:

go get -u github.com/go-kit/log@main

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:

go get -u=patch github.com/go-kit/log
and the second, using @patch version query:
go get -u github.com/go-kit/log@patch

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.


Thank you for being on our site 😊. If you like our tutorials and examples, please consider supporting us with a cup of coffee and we'll turn it into more great Go examples.

Have a great day!

Buy Me A Coffee

🏃‍♂️ What is 'go get' command in Go

Learn how to use and understand what the go get command does
go get go module dictionary