Using go dep to manage dependencies

Douglas Fernandes
bawilabs
Published in
2 min readMay 1, 2018

In this article, we’ll look at how to use a tool called Dep to manage the dependencies of your Go’s projects.

Why dep?

Dep is a dependency management tool, defined in the documentation itself as an “official experiment” for golang. Through it you can control the dependencies of your project without much work and define specific versions to guarantee stability in your project.

Without using a dependency manager you may come across many problems when developing multiple projects ,such as discovering that a particular dependency used on multiple projects ends up breaking them because of a small change that rename the method’s signature.

Managing dependencies may not be the first thing you are paying attention to, but the time will come when it will be inevitable to face this task.

Installation

curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh# Only in Bawilabs
cd $GOPATH/src
for folder in $(ls | grep -v gitlab); do echo $folder && rm -rf $folder; done

Install deps

# run in root directory
dep init

When you run the dep init command, the tool does:

  1. It identifies the dependencies of your current project;
  2. It validates whether or not these dependencies use the dep tool;
  3. It picks the highest compatible version for each of these dependencies.

When you finish executing this command, two files and a directory will be generated. The first Gopkg.toml file is where its dependencies are specified, it’s like package.json or pom.xml, if you have experience in nodejs or java, respectively. The second Gopkg.lock file is a snapshot of your project dependencies, or a list of all dependencies. And finally the vendor/ directory, where its dependencies are stored, is the equivalent of the node_modules/ directory in nodejs projects.

If you want to add new dependencies in the future, just use the following command:

dep ensure -add github.com/foo/bar@^2.0.0

Should I commit my vendor directory?

Nope.

Conclusion

Going back to the initial question, it is not mandatory to use it, but setting dependency versions of your project avoids good headaches and ensure that all squad members are running the same build on their machines.

I hope this article has shown you what it takes to get you started using dep.

If you need more help leave a comment :)

--

--

Douglas Fernandes
bawilabs

Geek apaixonado por café e pelo tricolor do Morumbi