Managing Dependencies with Govendor (Heroku, Dokku)

govendor is a dependency manager for Go. Go get gets you pretty far when it comes to installing dependencies at first, but when you want to deploy your project or when you want to pin the right version of your dependency it's not necessarily appropriate to use.

With govendor you can, like with npm or other tools specify which version of your dependency your project uses, which is a huge benefit for systems that are required to be somewhat stable.

Installing Govendor

As usual, make sure your GOPATH and similar are set correctly. Next step is to install govendor with go get:

go get -u github.com/kardianos/govendor

To test if the installation succeed, open a terminal and type govendor. If the output is not a long list of options, but a "command not found", you should have a look at your .bashrc and see if your GOPATH is set correctly.

Using Govendor with for your Go Dependencies

In order to list all the packages and versions of go packages you are currently using, you can simply run

govendor list

Which will produce an output with something like:

 e  github.com/PuerkitoBio/goquery         
 e  github.com/andybalholm/cascadia        
 e  github.com/araddon/dateparse           
 e  github.com/dustin/go-humanize          
 e  github.com/gin-contrib/cors            
 e  github.com/gin-contrib/sse             
 e  github.com/gin-contrib/static          
 e  github.com/gin-gonic/gin               
 e  github.com/gin-gonic/gin/binding       
 e  github.com/gin-gonic/gin/json          
 e  github.com/gin-gonic/gin/render        
 e  github.com/golang/protobuf/proto       
 e  github.com/json-iterator/go            
 e  github.com/mattn/go-isatty             
 e  github.com/ugorji/go/codec             
 e  golang.org/x/net/html                  
 e  golang.org/x/net/html/atom             
 e  golang.org/x/sys/unix                  
 e  gopkg.in/go-playground/validator.v8    
 e  gopkg.in/yaml.v2                       
pl  $YOURPACKAGE

Where $YOURPACKAGE probably is the name of your directory.

The e as a prefix means that all of these dependencies are external and not pinned to your project yet.

Running

govendor add +external

Will add them to your dependency file. When you list your dependencies again will now start with a v instead of an e.

Later your vendor.json can be used to install the correct versions using:

govendor sync

Will install the packages at the right versions that are stated in your vendor/vendor.json file that might look like this:

{
    "comment": "",
    "ignore": "test",
    "package": [
        {
            "checksumSHA1": "OfHbisNes0gI+S16gYBreh7Riv8=",
            "path": "github.com/PuerkitoBio/goquery",
            "revision": "152b1a2c8f5d0340f658bb656032a39b94e52958",
            "revisionTime": "2016-08-31T16:22:11Z"
        },
        {
            "checksumSHA1": "oLYnEHr1dTmo+QYudDHJJWNZ0a4=",
            "path": "github.com/andybalholm/cascadia",
            "revision": "1c31af6f6c1a7b101ed05aacc7d8a738b43ae86e",
            "revisionTime": "2016-08-31T15:30:56Z"
        },
        {
            "checksumSHA1": "XeZ0s/EYoIORsoatbSOJQzIDLgU=",
            "path": "github.com/gin-contrib/cors",
            "revision": "51f0ef8b07e973afb8b6c192bf90d5c3e575c01a",
            "revisionTime": "2017-09-17T02:20:32Z"
        },
        {
            "checksumSHA1": "QeKwBtN2df+j+4stw3bQJ6yO4EY=",
            "path": "github.com/gin-contrib/sse",
            "revision": "22d885f9ecc78bf4ee5d72b937e4bbcdc58e8cae",
            "revisionTime": "2017-01-09T09:34:21Z"
        },
        {
            "checksumSHA1": "WBCkV1yTg/nbmiEcQ9Q3iW93a5o=",
            "path": "github.com/gin-contrib/static",
            "revision": "e6b7eb92648d0a5faeea81700668a7a5dbe8424f",
            "revisionTime": "2017-09-17T02:08:54Z"
    }
  ],
    "rootPath": "test.jonathanmh.com"
}

Govendor Dependencies for Heroku or Dokku

If you're using Docker, Dokku or Heroku you'll probably want to run govendor sync at some point before compiling your binary inside your container or use a buildpack that supports it. Also Heroku has a nice help page about govendor.

Note that using godep you'll have to check in your dependencies directory at the moment, but with govendor you can just keep the file around and your container will do the installation for you.

To ignore the dependencies in your local project, you can add vendor/*/ to your .gitignore file.

I struggled with missing go dependencies a bit when I was trying out Dokku the first time.

Summary

That's as easy as it gets and kind of an NPM style dependency management option for Go. How do you manage your deps in Golang? Any comments or anything I could do much better? Let me know in the comments or on twitter. :)

Tagged with: #Dokku #go #golang

Thank you for reading! If you have any comments, additions or questions, please tweet or toot them at me!