JJ_programmer
JJ_programmer

Reputation: 632

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

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.

Upvotes: 61

Views: 81557

Answers (1)

bcmills
bcmills

Reputation: 5197

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.

Upvotes: 84

Related Questions