Go Modules Reference: Everything You Need to Know About Downloading go mod
How to Download Go Mod
Go mod is a tool that helps you manage dependencies in your Go projects. It allows you to specify the modules and versions that your code requires in a go.mod file, and then automatically downloads them to your local module cache. This way, you can ensure that your code builds and runs consistently across different environments.
how to download go mod
In this article, you will learn how to use go mod download to fetch the modules specified in your go.mod file. You will also learn how to download specific modules or versions, and how to troubleshoot common errors with go mod download.
Before you can use go mod download, you need to have the following:
A Go installation of version 1.11 or later. You can check your Go version by running go version.
A module path for your project. This is usually the repository location from which your code can be downloaded by Go tools, such as example.com/mymodule. You can set your module path by running go mod init in your project directory.
A go.mod file that defines your module's properties and dependencies. You can create a go.mod file by running go mod init, or edit it manually. For more information on go.mod files, see .
Downloading Dependencies with Go Mod
To download all the dependencies listed in your go.mod file, you can use the following command:
$ go mod download
This command will fetch the modules from their sources (such as version control repositories or proxy servers) and store them in your local module cache. The default location for the module cache is $GOPATH/pkg/mod, where $GOPATH is your Go workspace directory.
Understanding the Output of Go Mod DownloadWhen you run go mod download, you will see some output that looks like this:
go: downloading example.com/mymodule v1.2.3 go: downloading github.com/some/dependency v0.1.0 firstname.lastname@example.org h1:ZmZmZmZmZmZmZmZmZmZmZmZm email@example.com/go.mod h1:YmYmYmYmYmYmYmYmYmYmYmYm firstname.lastname@example.org h1:XnXnXnXnXnXnXnXnXnXnXnXn email@example.com/go.mod h1:WlWlWlWlWlWlWlWlWlWlWlWl
This output shows the following information for each module:
How to install go modules in GoLang
How to use go mod init command to create a go.mod file
How to manage dependencies with go mod tidy and go mod edit
How to upgrade or downgrade go modules with go get command
How to use go mod vendor to create a vendor directory
How to use go mod verify to check the integrity of go modules
How to use go mod why to find out why a module is needed
How to use go mod graph to visualize the dependency graph of go modules
How to use go mod download to download the source code of go modules
How to use semantic versioning for go modules
How to use pseudo-versions for go modules that are not tagged
How to use major version suffixes for go modules with breaking changes
How to use replace directive to replace a module with another version or a local directory
How to use exclude directive to exclude a specific version of a module from the build
How to use retract directive to mark a module version as deprecated or insecure
How to use minimal version selection algorithm for resolving module versions
How to use lazy module loading for faster builds and smaller binaries
How to use workspaces for working on multiple modules at once
How to use go work init command to create a go.work file
How to use go work edit command to add or remove modules from a workspace
How to use go work use command to switch between workspaces
How to use go work sync command to synchronize the workspace with the main module
How to use module proxies for faster and more reliable module downloads
How to use GOPROXY environment variable to configure the module proxy servers
How to use GOPRIVATE environment variable to bypass the proxy for private modules
How to use GONOPROXY and GONOSUMDB environment variables for finer control over proxy settings
How to use GOVCS environment variable to control the version control system used for fetching modules
How to serve modules directly from a proxy server using the GOPROXY protocol
How to communicate with proxies using HTTP requests and responses
How to find a repository for a module path using version control systems
How to map versions to commits and vice versa using version control systems
How to map branches and commits to versions using version control systems
How to locate module directories within a repository using version control systems
How to handle special cases for LICENSE files in module repositories
How to control version control tools with GOVCS environment variable
How to create module zip files for serving modules from a proxy server or storing them in the module cache
How to follow file path and size constraints for module zip files
How to authenticate modules using HTTPS or SSH protocols
How to pass credentials to private proxies or private repositories using netrc or ssh-agent mechanisms
How to protect privacy of module paths and versions using checksum database or local verification methods
How to use the module cache for storing and retrieving module files locally
How to use go.sum files for verifying the cryptographic checksums of module files
How to use checksum database for verifying the checksums of public modules online
How to use GOSUMDB environment variable for configuring the checksum database server
How to use GONOSUMDB environment variable for disabling the checksum database verification for some modules
How do I download Go Modules Reference documentation as PDF?
The module version, such as v1.2.3 or v0.1.0. This is the version that matches the requirement in your go.mod file, or the latest available version if no requirement is specified.
The module checksum, such as h1:ZmZmZmZmZmZmZmZmZmZmZmZm or h1:XnXnXnXnXnXnXnXnXnXnXnXn. This is a cryptographic hash of the module content, used to verify its integrity and authenticity.
The module go.mod checksum, such as h1:YmYmYmYmYmYmYmYmYmYmYmYm or h1:WlWlWlWlWlWlWlWlWlWlWlWl. This is a cryptographic hash of the module's go.mod file, which defines its properties and dependencies.
Using Go Mod Download with Flags
You can also use some optional flags with the go mod download command to modify its behavior or output. Here are some of the most useful flags:
-x: This flag prints the commands that go mod download executes, such as git fetch or go mod tidy.
-json: This flag prints the output in JSON format, which can be useful for parsing or processing by other tools.
-dir: This flag specifies a different directory for the module cache, instead of the default $GOPATH/pkg/mod.
For example, you can run the following command to download the modules in JSON format and store them in a custom directory:
$ go mod download -json -dir /tmp/modcache
Downloading Specific Modules with Go Mod
Sometimes, you may want to download specific modules or versions, instead of all the modules listed in your go.mod file. You can do this by passing one or more arguments to the go mod download command. The arguments can be module paths, versions, or version queries.
Downloading Modules by Path
To download a module by its module path, you can simply pass the path as an argument to the go mod download command. For example, to download the module rsc.io/quote, you can run:
$ go mod download rsc.io/quote
This will download the latest available version of the module, unless you have a specific requirement for it in your go.mod file.
Downloading Modules by Version
To download a module by its version, you can append the version to the module path with an @ sign. For example, to download version v1.5.2 of the module rsc .io/quote, you can run:
$ go mod download firstname.lastname@example.org
This will download the exact version of the module, regardless of the requirement in your go.mod file. You can also use the special version latest to download the latest available version of the module.
Downloading Modules by Version Query
To download a module by a version query, you can use a syntax similar to the one used by the go get command. A version query can be a prefix, a suffix, or a range of versions. For example, to download the latest minor version of the module rsc.io/quote in the v1 series, you can run:
$ go mod download rsc.io/quote@v1
This will download the highest version with the prefix v1, such as v1.5.2. You can also use the suffix @patch to download the latest patch version of the module, such as v1.5.3. For more information on version qu