Thursday, April 25, 2024
HomeGolangWhat's 'go get' command in Go (Golang)

What’s ‘go get’ command in Go (Golang)



In Go, so as to add a brand new exterior bundle to your venture, you could first obtain it. You are able to do this by utilizing the built-in go get command.

The go get command is a device used to obtain dependencies together with updating the go.mod file. It’s a part of the Go toolchain and comes pre-installed with the language. Utilizing this command, you’ll be able to obtain Go packages to your venture from distant repositories equivalent to GitHub, Bitbucket or different Git-based repositories.

Vital notice

The newest variations of the go get command work solely with Go modules. It implies that go get won’t work in case your venture is just not a Go module created with the go mod init command and doesn’t have a go.mod file.

Fundamental utilization

Fundamental syntax of the go get command:

go get [flags] [packages]

For extra info on flags and bundle naming, verify the documentation of the command utilizing the next command in terminal:

or on the official documentation website right here. On this submit we are going to give attention to utilization examples.

Examples

Bear in mind: Each time you run the go get command, it is usually good observe to run the command:

This command ensures that the go.mod file matches the supply code and cleans the go.mod and go.sum of unused and pointless dependencies created when upgrading packages to new variations.

Get a bundle

Use go get adopted by the bundle identify so as to add the bundle to your Go module.

go get github.com/google/uuid

Get a number of packages without delay

Use space-separated bundle names to obtain a number of packages without delay.

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

Replace bundle to the most recent model

To replace the bundle to the most recent model, use the express -u flag:

go get -u github.com/google/uuid

It’s also possible to use the usual go get command, which additionally updates the bundle to the most recent model, if out there:

go get github.com/google/uuid

Get a selected model of the bundle

To make use of a selected bundle model or to improve or downgrade a bundle model, use the @model question suffix:

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

Replace bundle to the most recent patch model

Use -u=patch flag to replace a bundle to the most recent patch model:

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

It’s also possible to use @patch model suffix to do the identical:

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

Replace bundle to a selected revision (commit hash)

It’s also possible to get a bundle in a selected commit model utilizing @revision question suffix:

go get -u github.com/google/uuid@ae25fc6Using question suffix @branch-or-tag-name, you'll be able to retrieve the bundle model from a selected department or tag:

Improve bundle to department or tag model

Utilizing question suffix @branch-or-tag-name, you’ll be able to retrieve the bundle model from a selected department or tag:

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

Improve all packages in your module without delay

If you wish to replace all of your venture’s dependencies without delay, use the command:

or:

Learn our submit on the right way to replace all Go packages to the most recent model to be taught extra.

Take away bundle out of your module

To fully take away a dependency from a Go module, use the go get with the suffix @none. Modules that depend upon the eliminated module will probably be downgraded or additionally eliminated:

go get github.com/google/uuid@none

The place go get downloads the venture’s dependencies from

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

  • Google’s managed module proxy server: https://proxy.golang.org/,
  • and if the module is just not out there within the Google proxy, then from the repository whose tackle is specified by the module path.

This habits is specified within the GOPROXY setting variable, which you’ll output with go env:

The default output:

https://proxy.golang.org,direct

If you wish to change the default habits of downloading from the module proxy and as a substitute use direct downloading of modules from repositories, you must set the setting variable, for instance, with the go env -w command:

First, nonetheless, it’s helpful to know what the module proxy is. You may examine it in the official weblog submit.

Utilizing the identical command, it’s also possible to set your individual proxy servers because the supply:

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

If you’d like, it’s also possible to change the default habits in a single command solely. All it’s good to do earlier than the go get command is to set the GOPROXY setting variable for the execution of the command:

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

The place go get places the venture’s dependencies in your native machine

The go get command downloads modules to a devoted cache. If one other venture wants a module with the identical model that’s already within the cache, it isn’t fetched once more however loaded from the cache. Packages are downloaded solely when they’re lacking from the cache.

The default location of the module cache is $GOPATH/pkg/mod. You may verify the precise path by printing the GOMODCACHE setting variable:

It’s also possible to change the default cache location by utilizing the go env -w command:

go env -w GOMODCACHE=~/mod-cache

The module cache has limitless dimension and its contents will not be eliminated robotically. However you’ll be able to erase its whole contents manually with the command:

Distinction between go get and go set up instructions

In earlier variations of Go, there was some inconsistency concerning the precise variations between the go get and go set up instructions. Presently, these instructions have their distinct obligations:

  • The go get command handles bundle administration – including, updating, or eradicating dependencies within the go.mod file. The go get doesn’t construct packages.
  • The go set up command builds the bundle and installs the executable file within the listing outlined by the GOBIN setting variable, which defaults to the $GOPATH/bin path. Putting in the executable file lets you name it straight out of your system terminal by merely typing the command identify, e.g. mytool within the command line.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments