Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between go get command and go mod download command

Tags:

go

go-modules

I'm trying to get a good understanding of Go modules and am a bit puzzled by the difference between the go get command and the go mod download command.

"The go get command updates module dependencies in the go.mod file for the main module, then builds and installs packages listed on the command line." https://golang.org/ref/mod#go-get

Whereas the Go mod download is described as :

"The go mod download command downloads the named modules into the module cache. " https://golang.org/ref/mod#go-mod-download

Clearly go get performs some dependency management which go mod download does not, but what is the difference between installing packages with go get and downloading modules to the module cache in go mod download.

like image 724
JJ_programmer Avatar asked Feb 24 '21 17:02

JJ_programmer


People also ask

What does go mod download do?

Go 1.11 introduces the go mod download command, which takes go. mod and go. sum files and downloads the dependencies from them instead of using the source code. As these files don't change frequently (unless you are updating the dependencies), they can be simply cached by the COPY command from Dockerfile.

Is go mod download necessary?

A go. mod file is required for the main module, and for any replacement module specified with a local file path. However, a module that lacks an explicit go. mod file may still be required as a dependency, or used as a replacement specified with a module path and version; see Compatibility with non-module repositories.

What is go mod command?

Go modules commonly consist of one project or library and contain a collection of Go packages that are then released together. Go modules solve many problems with GOPATH , the original system, by allowing users to put their project code in their chosen directory and specify versions of dependencies for each module.

What is the go GET command?

In Go, you can use the get command to download and install packages and dependencies. Azure Repos Git provides support for go get within an Azure Repos Git repository. With go get , you will be able to download packages with their dependencies named by the import paths.


1 Answers

Your module's go.mod file records which versions of dependencies it requires. The source code for those dependencies is stored in a local cache.

go get updates the requirements listed in your go.mod file. It also ensures that those requirements are self-consistent, and adds new requirements as needed so that every package imported by the packages you named on the command line is provided by some module in your requirements.

As a side-effect of updating and adding requirements, go get also downloads the modules containing the named packages (and their dependencies) to the local module cache.

In contrast, go mod download does not add new requirements or update existing requirements. (At most, it will ensure that the existing requirements are self-consistent, which can occur if you have hand-edited the go.mod file.) It only downloads either the specific module versions you've requested (if you requested specific versions), or the versions of modules that appear in your requirements.

like image 191
bcmills Avatar answered Nov 11 '22 05:11

bcmills