- Published on
Setting up Go's dep Tool to manage dependencies
- Authors
- Name
- Yair Mark
- @yairmark
As discussed in my first post about Go, out of the box Go does not have a clear way of managing dependencies like Node's npm for example.
The official recommended way of managing dependencies in Go is to use the dep framework. The trickiest thing about this is that dep seems to be very opinionated about where your Go projects should live. But with this enforced convention comes some really great automation. Once set up properly you go into the root of your project and run dep init
. This then looks through the imports on your GOPATH, works out what dependencies you need and created a toml with these dependencies and fixed versions (where possible). This also creates a npm style lock file for consistent build across machines. More details on these mechanics can be found here.
Setting up Dep
Environment Variable Setup
To setup dep properly I had to first set the below environment variables in my .zshrc file:
### Go ###
export GO_HOME="/usr/lib/go-1.9/bin"
export GOPATH="/home/youruser/code/go"
export GOROOT="/usr/local/go"
export PATH=$PATH:~/bin:$GOROOT/bin:$GOPATH/bin
In the above:
$GOROOT/bin
: puts the core go binaries and development tools onto your path$GOPATH/bin
: anything yougo get url/package
which installs as a binary and is placed underbin
will be on your path
Your GOPATH can refer to any path you want but should generally stay fixed to one place as any binaries that you go get will have to be retrieved again.
Installing dep
Once this is done install dep
as described here
dep lifecycle
Bootstrapping a Project
You need to setup the initial folder structure of your project that must conform with deps conventions as described here and as illustrated below:
mkdir -p $GOPATH/src/github.com/me/example
cd $GOPATH/src/github.com/me/example
Just to illustrate what dep init does before running init I listed my new directory:
> ls -R $GOPATH
ls -R $GOPATH
/home/youruser/code/go:
src
/home/youruser/code/go/src:
github.com
/home/youruser/code/go/src/github.com:
me
/home/youruser/code/go/src/github.com/me:
example
/home/youruser/code/go/src/github.com/me/example:
Then from $GOPATH/src/github.com/me/example
I ran dep init
:
ls -R $GOPATH
/home/youruser/code/go:
pkg src
/home/youruser/code/go/pkg:
dep
/home/youruser/code/go/pkg/dep:
sources
/home/youruser/code/go/pkg/dep/sources:
/home/youruser/code/go/src:
github.com
/home/youruser/code/go/src/github.com:
me
/home/youruser/code/go/src/github.com/me:
example
/home/youruser/code/go/src/github.com/me/example:
Gopkg.lock Gopkg.toml vendor
/home/youruser/code/go/src/github.com/me/example/vendor:
Adding a New Dependency to a Project
Once you have dep setup for a project you can get it to add new dependencies for you from the root of the project by running: dep ensure -add sourceUrl/package/path
If you get an error like no dirs contained any Go code
you will need to create a main.go
file (touch main.go
) and add a main method to it. dep ensure
seems to need at least one Go file to exist before it adds dependencies to your toml and lock files.
package main
function main() {
}
Removing a Dependency from a Project
If you need to remove a dependency you:
- Remove usages of that dependency from the code
- Run
dep ensure
from the root of the project- This will automatically remove the dependency from the lock file and vendor folder
- You will have to manually remove the dependency from the toml file