XenonStack

A Stack Innovator

Post Top Ad

Friday 15 September 2017


Introduction

GO is a new language and thanks to GO community, it has grown rapidly since the release of GO 1.0. yet, keeping its most fundamental principle of simplicity intact.

Like other programming languages, GO has the approach for dependency management like npm in Node.js, the pip in Python.

GO user and developer have been using go get all the while. And there were many tools like godep, glide etc. available to compensate the little shortcomings of go get tool. One of the tool is glide and we will see how to use glide for package management.

But in future GO community might see a better dependency tool currently under development github.com/golang/dep.

Here I will walk through how we use different tools for managing packages in GO.
Why GoLang is Used

Workspace

When we set up Golang, we have a single directory where $GOPATH is referencing to. This directory is our workspace, where all the thing about go begins. And the workspace structure looks like

-Work
|
-- bin/
|
-- pkg/
|
-- src/
|
-- project1/
|
-- project2/

bin - It contains executable commands.
pkg - It contains package objects.
src - It contains source files.


$GOPATH

It is an environment variable which specifies the location of our workspace. Inside the directory $GOPATH/src we create a new directory for each project we start to work on.

This $GOPATH exists because:
  • All the import declaration in GO code references to a package through import path, and directories inside $GOPATH/src where go tool can compute all the imported packages.

  • To store the dependencies retrieved by go get.


Simplicity of GO GET

go get is the official GO tool to fetch GO code from a repository and store it in $GOPATH/src.

It provides isolation of packages with different import paths.

And in our source code, we just have to specify our compiler where it should go to get latest sources.
import
(
“fmt”
“github.com/gorilla/mux”
)
go get github.com/gorilla/mux
Dependency Management in GoLang

And it will install the latest commit from the master branch of GitHub repository, these packages are not limited to GitHub.

For example golang.org/x/mobile - libraries and build tools for GO on Android.
go get always fetches the latest code for any package which is not already on the disk.


GO GET Flags

go get also uses flag instruction such as [-u, -insecure, -d, -f, -t, -fix, -v.]
The -u flag instructs get to use the network to update the named packages and their dependencies.

go get -u github.com/gorilla/mux
The -insecure flag permits fetching from repositories and resolving custom domains using insecure schemes such as HTTP.

go get -insecure github.com/name/repo
The -d flag instructs get to stop after downloading the packages; that is, it instructs get not to install the packages.

The -f flag, valid only when -u is set, forces get -u not to verify that each package has been checked out from the source control repository implied by its import path. This can be useful if the source is a local fork of the original.

The -fix flag instructs the get to run the fix tool on the downloaded packages before resolving dependencies or building the codes.

The -t flag instructs get to also download the packages required to build the tests for the specified packages.


Shortcomings in GO GET

We see that the way GO saves all its dependencies, there are some problems in this approach of dependency management like:
  • We won't be able to determine which version of package we need unless it is hosted in complete different repository as go get always fetches from the latest version of a package. This leads to problem when working as team we might end up fetching different version of a package.

  • And since go get installs package at $GOPATH/src directory so we will have to use the same version of a package for different projects as all our projects are under a single directory. So each of the projects will not have different versions of dependencies with them.


Package Management Using Glide

Now that we have seen how GO handles imports and manage packages and we also saw some of the difficulties a developer faces when handling dependencies. Let us tell you how to solve them.

There are many tools available to handle packages which have been used by GO developers like godep, glide etc. and we are going to explain the one we are using : GLIDE.


What is Glide?

Glide is a package management tool for GO language. It downloads dependencies from different sources and then locks the versions so that each team member gets an exact same version to download and updates the dependencies which do not break the project.
Glide in GoLang

Installing Glide

  • At first, we will install glide, using shell script curl https://glide.sh/get | sh
It will get the latest release of glide and the script puts it in GO binaries ($GOPATH/bin or $GOBIN).

Continue Reading The Full Article at - Xenonstack.com/Blog

No comments:

Post a Comment