Cookies management by TermsFeed Cookie Consent

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

go get go module dictionary

In Go, to add a new external package to your project, you must first download it. You can do this by using the built-in go get command.

The go get command is a tool used to download dependencies along with updating the go.mod file. It is part of the Go toolchain and comes pre-installed with the language. Using this command, you can download Go packages for your project from remote repositories such as GitHub, Bitbucket or other Git-based repositories.

Important note

The latest versions of the go get command work only with Go modules. It means that go get will not work if your project is not a Go module created with the go mod init command and does not have a go.mod file.

Basic usage

Basic syntax of the go get command:

go get [flags] [packages]

For more information on flags and package naming, check the documentation of the command using the following command in terminal:

 go help get

or on the official documentation site here. In this post we will focus on usage examples.

Examples

Remember: Every time you run the go get command, it is also good practice to run the command:

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 upgrading packages to new versions.

Get a package

Use go get followed by the package name to add the package to your Go module.

go get github.com/google/uuid

Get multiple packages at once

Use space-separated package names to download multiple packages at once.

go get github.com/google/uuid gorm.io/gorm

Update package to the latest version

To update the package to the latest version, use the explicit -u flag:

go get -u github.com/google/uuid

You can also use the standard go get command, which also updates the package to the latest version, if available:

go get github.com/google/uuid

Get a specific version of the package

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

go get github.com/google/uuid@v1.3.0

Update package to the latest patch version

Use -u=patch flag to update a package to the latest patch version:

go get -u=patch github.com/google/uuid

You can also use @patch version suffix to do the same:

go get -u github.com/google/uuid@patch

Update package to a specific revision (commit hash)

You can also get a package in a specific commit version using @revision query suffix:

go get -u github.com/google/uuid@ae25fc6Using query suffix @branch-or-tag-name, you can retrieve the package version from a specific branch or tag:

Upgrade package to branch or tag version

Using query suffix @branch-or-tag-name, you can retrieve the package version from a specific branch or tag:

go get -u github.com/google/uuid@master

Upgrade all packages in your module at once

If you want to update all your project’s dependencies at once, use the command:

go get -u ./...

or:

go get -u all

Read our post on how to update all Go packages to the latest version to learn more.

Remove package from your module

To completely remove a dependency from a Go module, use the go get with the suffix @none. Modules that depend on the removed module will be downgraded or also removed:

go get github.com/google/uuid@none

Where go get downloads the project’s dependencies from

By default, the go get command retrieves dependencies from two sources:

This behavior is specified in the GOPROXY environment variable, which you can output with go env:

go env GOPROXY

The default output:

https://proxy.golang.org,direct

If you want to change the default behavior of downloading from the module proxy and instead use direct downloading of modules from repositories, you should set the environment variable, for example, with the go env -w command:

go env -w GOPROXY=direct

First, however, it is useful to know what the module proxy is. You can read about it in the official blog post.

Using the same command, you can also set your own proxy servers as the source:

go env -w GOPROXY=https://proxy.example.com,https://proxy2.example.com,direct

If you want, you can also change the default behavior in a single command only. All you need to do before the go get command is to set the GOPROXY environment variable for the execution of the command:

GOPROXY=direct go get github.com/google/uuid

Where go get puts the project’s dependencies on your local machine

The go get command downloads modules to a dedicated cache. If another project needs a module with the same version that is already in the cache, it is not fetched again but loaded from the cache. Packages are downloaded only when they are missing from the cache.

The default location of the module cache is $GOPATH/pkg/mod. You can check the exact path by printing the GOMODCACHE environment variable:

$GOPATH/pkg/mod

You can also change the default cache location by using the go env -w command:

go env -w GOMODCACHE=~/mod-cache

The module cache has unlimited size and its contents are not removed automatically. But you can erase its entire contents manually with the command:

 go clean -modcache

Difference between go get and go install commands

In earlier versions of Go, there was some inconsistency regarding the exact differences between the go get and go install commands. Currently, these commands have their distinct responsibilities:


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

🆕 How to update all Go packages to the latest version

Upgrade all your project’s dependencies using the go get command
go get go module

🫶 The 'main()' function, 'main' package and 'main.go' file in Go

Learn how to create an executable Go program
dictionary