Modules were introduced in Go v1.11
to address dependency management, code organization, and versioning issues.
A module is a collection of related Go packages that are versioned together as a single unit. A module is identified by its module path, which is typically a URL where the module's source code is hosted.
go.mod
file defines the module's path and lists its dependencies along with their versions.
Devs can manually edit this file, but Go tooling also provides commands to manage it automatically.
Dependencies are other modules that our module relies on. They are specified in the go.mod
file along with the version constraints.
Go modules are downloaded from module proxies, which are HTTP servers that cache and serve module versions. The Go toolchain provides the GOPROXY environment variable to configure which module proxy to use.
Go modules are cached on the local filesystem to avoid redundant downloads. By default, the module cache is located in the $GOPATH/pkg/mod
directory.
Go commands like go build
, go test
, and go get
are module-aware. They automatically work with modules and their dependencies, resolving versions and downloading dependencies.
-
go mod init
: Initializes a new module in the current directory. This command creates a new go.mod file if one doesn't already exist. -
go mod tidy
: Analyzes your codebase and adds any missing dependencies to the go.mod file. It also removes any unnecessary dependencies.Flags:
-v
: Verbose mode, prints additional information about the changes made.
-
go mod vendor
: Copies dependencies from the module cache to the vendor directory within the project. (Example: if we want to include dependencies within our project's repository).Flags:
-v
: Verbose mode, prints additional information about the copied files.-e
: Exclude vendor directory when resolving imports.
-
go mod download
: Downloads the dependencies specified in the go.mod file. It doesn't install the dependencies, but rather downloads them into the module cache.Flags:
-json
: Prints output in JSON format.-x
: Print commands as they are executed.
-
go mod verify
: Checks that the dependencies in the go.mod file have not been tampered with. It verifies the checksums of downloaded modules against those recorded in the go.sum file. -
go mod graph
: Prints the module dependency graph, showing the module dependencies in a textual format.Flags:
-json
: Prints the graph in JSON format.
-
go mod edit
: Provides various subcommands to edit the go.mod file. For example, you can use go mod edit -require=@ to add a requirement to the go.mod file. -
go mod why
: Shows an explanation for why a particular module or package is needed in the module graph.Flags:
-m
: Prints only the main module requirement.
-
go mod list
: Lists all dependencies of the current module along with their versions.Flags:
-json
: Prints output in JSON format.
-
go mod replace
: Allows you to specify replacements for specific module versions. This can be useful for testing or development purposes.