The Go get tool
Go get is a tool to get third party projects from CVS repositories. Instead of using the git clone
command, you can use Go get to receive a series of added benefits. Let's write an example using CoreOS's ETCD project which is a famous distributed key-value store.
CoreOS's ETCD is hosted on GitHub at https://github.com/coreos/etcd.git. To download this project source code using the Go get tool, we must type in the Terminal it's resulting import path that it will have in our GOPATH:
$ go get github.com/coreos/etcd
Note that we have just typed the most relevant information so that Go get figures out the rest. You'll get some output, depending on the state of the project, but after, while, it will disappear. But what did happen?
- Go get has created a folder in
$GOPATH/src/github.com/coreos
. - It has cloned the project in that location, so now the source code of ETCD is available at
$GOPATH/src/github.com/coreos/etcd
. - Go get has cloned any repository that ETCD could need.
- It has tried to install the project if it is not a library. This means, it has generated a binary file of ETCD and has put it in
$GOPATH/bin
folder.
By simply typing the go get [project]
command, you'll get all that material from a project in your system. Then in your Go apps, you can just use any library by importing the path within the source. So for the ETCD project, it will be:
import "github.com/coreos/etcd"
It's very important that you get familiar with the use of the Go get tool and stop using git clone
when you want a project from a Git repository. This will save you some headaches when trying to import a project that isn't contained within your GOPATH.