Manage golang dependencies
Using golang in these 2 months I think the most hard thing is manage dependencies, when I use golang only to test or have fun, normally, I don’t worries with dependencies problems. On Ruby, Java, Python and Nodejs ecosystem have (good) ways to manage dependencies like bundler, maven, virtualenv and npm. Let’s see a little bit about golang dependencies.
There is a initial doc about dependencies in golang.org called How to Write Go Code, the doc say how works the golang workspace, the basic is when we runs
go get the go will download and install the packages into $GOPATH, for more details see cmd/go/get.go. The problems with this approach is about the versions of third-party library, for many times I asked: “My code is OK, now how do I guarantee that the third-party libraries I used will stay with the same API?”, I saw some ways to fix it, but none worked fine, like fork the library repository, I wanted only work in my project and don’t maintain the third-party libraries dependences.
Well, searching a golang buildpack to heroku, I found godep a ‘dependency tool for go’, godep is very useful and simple tool, you only need a quick reading in the README to learn how to work with the tool.
On development environment I will always run:
To search the third-party libraries in project, and save the current versions of them. Godep will use the current commit id of the library and saves it on a file called Godeps.json, see a example uhura Godeps.json). To run the code with saved versions there is command:
godep go ..., like:
godep go test
This will run the
go test with saved versions of the third-party libraries, even if they have changed into $GOPATH.
On production environment, to each deploy per example, I run:
Basically, godep reads the Godeps.json, and restore the libraries exactly on specified version. And now I can be sure my code will work with the third-party dependencies.
To install and learn more, please visit github.com/kr/godep.
That’s all folks! Feel free to send question or suggestion, Thanks!