36

Migrating Projects from Dep to Go Modules

 5 years ago
source link: https://www.tuicool.com/articles/hit/beuARrm
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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 usingdepto 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 fromdepright out of the box. Woot!

Now you should see a new file calledgo.modwhich looks something like this:

module github.com/kounta/luigi

go 1.12

require (

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 morerequires, but I removed most of them to keep it brief.

Likedepthat has a separate toml and lock file, we need to generate thego.sumfile. Simple run (this also validates everything is OK):

go build

Now you can remove theGopkg.lockandGopkg.tomland commit the newgo.modandgo.sumfiles.

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:

  1. Log into your Github account and goto Settings > Personal access tokens.

  2. Create a new token and make sure you check the permission to allow it access to private repositories.

  3. Then run:

export GITHUB_TOKEN=xxx

git config --global url."https://${GITHUB_TOKEN}:[email protected]/kounta".insteadOf "https://github.com/kounta"


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK