GoLang: Glide for Go language package management

Downloading 3rd party packages from github is made very simple in the Go language with the import statement. But similar to other languages, the complexity of versions and inter-dependencies begs the use of a package manager for any projects that are non-trivial (think npm for Javascript, pip for Python, Maven for Java, etc.).

Glide is a package manager for the Go programming language that can greatly ease the chore of package management by supporting package independence between projects, versioning, and non-master branches.

Installation

On Ubuntu 14.04-16.04 you can install from a ppa:

$ sudo add-apt-repository ppa:masterminds/glide && sudo apt-get update
$ sudo apt-get install glide

Example projects

I have created two Go projects on github, go-vendortest1 and go-myutil. The “vendortest” project calls the method in the “myutil” package as shown below:

package main

import (
	"fmt"
	"github.com/fabianlee/go-myutil/myutil"
)

func main() {
	fmt.Println("Version: ",myutil.GetBranch())
}

The “myutil” package is even simpler, and just returns a static string.

package myutil

func GetBranch() string {
  return "master"
}

Now download this project and and initialize glide:

$ mkdir -p $GOPATH/src
$ cd $GOPATH/src
$ git clone https://github.com/fabianlee/go-vendortest1.git
$ cd go-vendortest1/vendortest
$ glide init --non-interactive

Now there is a file named “glide.yaml” in the project directory, and we need to manually give it a hint by adding “version: master” at the same indentation level as package like below:

package: go-vendortest1/vendortest
import:
  - package: github.com/fabianlee/go-myutil
    version: master
    subpackages:
    - myutil

To instruct glide to fetch the dependencies run “glide up”:

$ glide up

INFO]	Downloading dependencies. Please wait...
[INFO]	--> Fetching updates for github.com/fabianlee/go-myutil.
[INFO]	Resolving imports
[INFO]	Downloading dependencies. Please wait...
[INFO]	Setting references for remaining imports
[INFO]	Exporting resolved dependencies...
[INFO]	--> Exporting github.com/fabianlee/go-myutil
[INFO]	Replacing existing vendor dependencies
[INFO]	Project relies on 1 dependencies.

And now a file named “glide.lock” is created along with a download of the 3rd party packages into the “vendor” directory.

Now install the dependencies with “glide install”:

$ glide install

[INFO]	Downloading dependencies. Please wait...
[INFO]	--> Found desired version locally github.com/fabianlee/go-myutil 5a23de6a0ad15d27e9ceb71a150219733e590dbc!
[INFO]	Setting references.
[INFO]	--> Setting version for github.com/fabianlee/go-myutil to 5a23de6a0ad15d27e9ceb71a150219733e590dbc.
[INFO]	Exporting resolved dependencies...
[INFO]	--> Exporting github.com/fabianlee/go-myutil
[INFO]	Replacing existing vendor dependencies

Now the workflow is the standard build and run:

$ go build
$ ./vendortest
Version: master

So far, everything is as expected and the 3rd party package is being pulled from the HEAD of the master branch.

Non-master branch

But, now take notice that the go-myutil package also has a second branch named “mybranch1“, and the ‘myutil/myutil.go‘ file is this branch just slightly altered to return the string ‘mybranch1’.

package myutil

func GetBranch() string {
  return "mybranch1"
}

However, this file is never going to be pulled by glide because it is not on the master branch we specified in “glide.yaml”.

Glide version

Now let’s tell Glide that we want to use the “mybranch1” branch of the myutil package.  Modify “glide.yaml” so it looks like:

package: go-vendortest1/vendortest
import:
- package: github.com/fabianlee/go-myutil
  version: mybranch1
  subpackages:
  - myutil

Then have glide update and install the branch version of the myutil package, and then build and run again.

$ glide up
$ glide install
$ go build
$ ./vendortest
Version:  mybranch1

As you can see, we are now using the non-master ‘mybranch1’ version of the 3rd party package.

 

 

REFERENCES

https://github.com/Masterminds/glide

https://glide.readthedocs.io/en/latest/glide.yaml/

https://glide.readthedocs.io/en/latest/commands/

https://blog.gopheracademy.com/advent-2015/glide/

https://ncona.com/2016/05/golang-sane-dependency-management-with-glide/