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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With