Migrating Projects from Dep to Go Modules
Go Modules are the future of Go package management. They have been available to try since Go 1.11, and will be the default behaviour from Go 1.13.
I’m not going to go over (pun-intended) how a package manager works in this article. I will be strictly talking about the path to upgrade existing projects using dep
to Go modules.
Spoiler alert… It’s easy!
In my examples I will be using our proprietary (and therefore private) package github.com/kounta/luigi (more on this later). It contains all sorts of top secret stuff that I can’t talk about. Let’s just say that it is used by several of our other projects written in Go. A perfect candidate.
First, we need to initialise the module:
cd github.com/kounta/luigi
go mod init github.com/kounta/luigi
Just two lines of output (but they are lovely):
go: creating new go.mod: module github.com/kounta/luigi
go: copying requirements from Gopkg.lock
Yep. That’s right. It’s already migrated all the dependencies from dep
right out of the box. Woot!
Now you should see a new file called go.mod
which looks something like this:
module github.com/kounta/luigigo 1.12require (
github.com/elliotchance/tf v1.5.0
github.com/gin-gonic/gin v1.3.0
github.com/go-redis/redis v6.15.0+incompatible
)
There were many more require
s, but I removed most of them to keep it brief.
Like dep
that has a separate .toml
and .lock
file, we need to generate the go.sumfile. Simple run (this also validates everything is OK):
go build
Now you can remove the Gopkg.lock
and Gopkg.toml
and commit the new go.mod
and go.sum
files.
Travis CI
If you are using Travis CI you will need to enable modules by setting an environment variable until it becomes the default setting in Go 1.13:
GO111MODULE=on
Private Repositories
If you need to import from a private repository you may see an error like:
invalid module version "v6.5.0": unknown revision v6.5.0
This is misleading. What it’s really trying to say is that it recognises what do with the URL (in this case it is github.com). However, it can’t find the repository because GitHub will not confirm its existence.
Fixing it is pretty straight forward:
- Log into your Github account and goto Settings > Personal access tokens.
- Create a new token and make sure you check the permission to allow it access to private repositories.
- Then run:
export GITHUB_TOKEN=xxx
git config — global url."https://${GITHUB_TOKEN}:x-oauth-basic@github.com/kounta".insteadOf "https://github.com/kounta"
Originally published at http://elliot.land on March 21, 2019.