Go Modules for Go Dep Users
Dependency management is a problem that is there with the programming languages. Most of the programming languages have their own solution to this problem. In Golang, currently, there are two dependency management solutions. Go Dep was introduced by the Go development team as
the “official experiment” dependency management tool for the Go language
So, most of the Go software projects started using Go Dep. But in later go versions, it was officially decided to go with Go Modules as the official dependency management tool for Golang. This article is intended to provide a high-level introduction to Go Modules in a Go Dep user’s point of view.
Initializing the project
In dep, the project initialization is done using the following command, which creates
vendor/ files and folders.
$ dep init
the same way we can initialize a go module project using ( in the project root)
$ go mod init
this command will create
go.mod file, which contains the dependency information like
Gopkg.toml in Dep.
According to the Go documentation, vendoring is interpreted as
using local copies of external dependencies to satisfy imports of those dependencies
Dep is considered as one of the most frequently used vendoring tools for Golang. Go modules also support vendoring. Go modules provides a very easy solution for vendoring (Copying the dependencies to a folder named
vendor). The following command copies all the packages required by the packages in the main module for building and testing.
$ go mod vendor
But most importantly it won’t copy the packages that are only imported by the tests of packages outside of the main module.
If you are already using Go Dep, you may recall that you needed to set up Go Dep separately (https://golang.github.io/dep/docs/installation.html). But one of the biggest advantages in Go Modules is that it comes embedded to the Go ecosystem. Basically you don’t have to separately install Go Modules. If you have a newer version of Go lang installed, that means you have Go Modules with you.
Placing your project files
With dep, locating your project files is not as easy as other languages like Java. You have to keep all the files under a strict path. According the Go Dep documentation, it’s stated that we have to place the project files under
But with Go Modules, we have the freedom of keeping the project files anywhere like Java. As of Go version 1.11, the Go commands enable the Go Modules usage if there is a
go.mod file present in the project root and the project directory is placed outside of the
$GOPATH/src . But if the project is under
$GOPATH/src, it will run under old GoPath mode even if a
go.mod file is present. From Go version 1.13, module mode will be the default.
In Dep, adding new dependencies requires running a command like,
$ dep ensure -add github.com/pkg/errors
But it has made much easier in Go Modules. Go modules has the ability to scan the project and automatically determine the required dependencies and add it to the
go.mod file. This scan will be triggered on running
go commands such as,
$ go build
$ go test
Let’s wrap up
This wasn’t a how-to guide on using Go Modules but a comparison between Go Dep and Go Modules. Hope you got an overall idea about the two dependency management tools. Feel free to raise any concerns you have.