Go Modules: Package Management Terminator
Introduction
Package management in Go has been a hotly debated topic in the Go community. Before Go 1.11, there was no official package management tool for Go, with the most popular third-party options being dep and glide. However, with the release of Go 1.11, the Go team introduced their official package management tool Go Modules which became the officially recommended package management tool as of Go 1.13.
Go Modules
Go Modules
is the official package management tool introduced in Go 1.11. With Go Modules
, you can manage dependencies anywhere, whether they are outside of $GOPATH
or in an offline environment. It also supports versioning, so you can ensure that you and your team are using exactly the same versions of your dependencies.
Using Go Modules
To enable Go Modules in a new project, first navigate to the project directory and then run the following command:
|
|
This command will create a go.mod
file, which contains your project’s module path and the minimum Go version requirement.
You can then run the go get
command to fetch dependencies:
|
|
This command will update both your go.mod
file and a go.sum
file. The go.mod
file will gain a new dependency, while the go.sum
file contains the expected cryptographic hashes of each dependency.
Dependency Versions
Dependency versioning is very straightforward in Go Modules. In the go.mod
file, you will see dependency versions that look something like this:
|
|
Here, v0.9.1
is the version of the dependency. Go Modules supports semantic versioning, as well as versioning for untagged repositories.
Common Commands
Initialize a new module
|
|
Example:
|
|
This command creates a new go.mod
file in the current directory with the module name and Go version.
Add missing and prune unused modules
|
|
This command automatically updates the go.mod
and go.sum
files, adding missing modules and removing unused modules.
Upgrade dependency modules to the latest version
|
|
Or you can specify the module and version:
|
|
Example:
|
|
View the modules and versions currently being used
|
|
This command shows a list of all direct and indirect dependencies along with their versions.
Download modules to the local cache
|
|
This command downloads all dependencies listed in the go.mod
file to the local cache.
These are just a few basic commands, Go Modules
has many other features and options, which you can learn more about by reading the official documentation or using the go help mod
command.
Other Package Managers
While Go Modules
is the officially recommended package management tool, there are a few other package managers for Go in the community, such as dep
and glide
. These tools were very popular before Go Modules
came along, but are no longer actively maintained by their original authors.
Conclusion
Go’s package management landscape has seen a few major changes over the years, but it has finally settled down. For new Go projects, it is recommended to use Go Modules
for package management, as it is the officially recommended tool and has the widest community support.