Working with Glide – Vendor Package Management for Go

In this post, we will use Glide to manage the dependencies of a Go project. Before starting, let me give you a brief introduction about Glide.

What is Glide?
Glide is a package manager for Go that is conceptually similar to package managers for other languages such as NPM for Node.js, Pip for Python, and so forth which records dependency information in a glide.yaml file and utilizes vendor/ directories, so that different projects can have different versions of the same dependencies.

Now, lets’ start with creating a small project with a single Go file which will give colourized outputs, following steps:

Step 1: Install Glide executing the following command:

$ curl https://glide.sh/get | sh

Above command will add glide binary in $GOPATH/bin.

Step 2: Move to $GOPATH/src to create a directory – golang-glide-example executing the following command:

$ cd $GOPATH/src && mkdir golang-glide-example

Step 3: Move to $GOPATH/src/golang-glide-example to create a Go file which will give colourized outputs executing the following command:

$ cd $GOPATH/src/golang-glide-example && touch color.go

Step 4: Copy the following code to color.go:

package main

import (
	"github.com/fatih/color"
)

func main() {
	color.Red("We have red")
	color.Blue("Prints %s in blue.", "text")
}

Step 5: Move to $GOPATH/src/golang-glide-example and execute the following command:

$ cd $GOPATH/src/golang-glide-example && glide init --non-interactive

Above command will create glide.yaml which contain all the dependencies to run the project.

Step 6: Move to $GOPATH/src/golang-glide-example, install all dependencies and build the project executing the following command:

$ cd $GOPATH/src/golang-glide-example && glide install && go build

Above command will download and export the dependencies and the output logs will look like as shown in a screenshot below:

Screen Shot 2018-04-14 at 12.20.06 PM

Step 7: Move to $GOPATH/src/golang-glide-example and Run golang-glide-example executing following command:

$ cd $GOPATH/src/golang-glide-example && ./golang-glide-example

which will give us the colourized output same as shown in a screenshot below:

Screen Shot 2018-04-14 at 12.20.24 PM

The complete source code is hosted on GitHub.

Advertisements