Contents

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:

1
go mod init [module path]

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:

1
go get github.com/pkg/errors

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:

1
require github.com/pkg/errors v0.9.1

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

1
go mod init <module-name>

Example:

1
go mod init github.com/username/myproject

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

1
go mod tidy

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

1
go get -u

Or you can specify the module and version:

1
go get <module-name>@<version>

Example:

1
go get github.com/gin-gonic/[email protected]

View the modules and versions currently being used

1
go list -m all

This command shows a list of all direct and indirect dependencies along with their versions.

Download modules to the local cache

1
go mod download

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.