# Manage Dependencies With GODEP

- Published: October 26, 2013
- Author: William Kennedy
- URL: https://www.ardanlabs.com/blog/2013/10/manage-dependencies-with-godep.html

<h3><b>Introduction</b></h3>
If you are using 3rd party packages, (packages that you don't own or control), you will want a way to create a reproducible build every time you build your projects. If you use 3rd party packages directly and the package authors change things, your projects could break. Even if things don't break, code changes could create inconsistent behavior and bugs.<br />
<br />
Keith Rarick's tool <b>godep</b>&nbsp;is a great step in the right direction for managing 3rd party dependencies and creating reproducible builds. The <b>godep</b> tool gives you two options for managing dependencies. The first option creates a dependency file with version control information and then with some <b>godep</b> magic,&nbsp;the code is built against those versions. You can also Vendor your 3rd party packages inside your projects as well. You never need to change a single source code file and everything is accomplished in conjunction with the go tooling.<br />
<h3>
<b><br />Downloading Godep</b></h3>
Download <b>godep</b>&nbsp;using go get and make sure your $GOPATH/bin directory is in your PATH.<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
go get github.com/kr/godep<br />
export PATH=$PATH:$GOPATH/bin</div>
<h3>
<b><br />Create A Project</b></h3>
Build your project using the 3rd party packages as you normally would. Since <b>godep</b> does not require you to change any import paths in the code, 'go get' the code you need and import those packages directly. To keep the post simple, I am going to use an existing program called News Search that uses one 3rd party dependency.<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
export GOPATH=$HOME/example<br />
go get github.com/goinggo/newssearch</div>
<br />
<div class="separator" style="clear: both; text-align: center;">

<a href="../../../images/goinggo/Screen+Shot+2013-10-26+at+8.41.33+AM.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;" target="_blank"><span class="sr-only">View linked image: Screen Shot</span>![Screen Shot](../../../images/goinggo/Screen+Shot+2013-10-26+at+8.41.33+AM.png)</a>

</div>
<br />
After 'go get' completes, I have the following code on disk inside the GOPATH.<br />
<br />
The News Search program is using code from a different Going Go repository, which for this project is a 3rd party package. Since 'go get' was successful, the code built and is installed.<br />
<div style="clear: both;">
</div>
<h3>
<b>Dependency Management</b></h3>
Once you have a project that can build and install properly, you can use&nbsp;<b>godep</b>&nbsp;to create the Godeps dependency file. Change to the root location for the project and run the&nbsp;<b>godep save</b>&nbsp;command with the <b>-copy=false</b> option:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
cd $GOPATH/src/github.com/goinggo/newssearch<br />
godep save -copy=false</div>
<br />
Once the save is complete,&nbsp;<b>godep</b>&nbsp;creates a file called Godeps. You will save this file with your project:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
{<br />
&nbsp; &nbsp; &quot;ImportPath&quot;: &quot;github.com/goinggo/newssearch&quot;,<br />
&nbsp; &nbsp; &quot;GoVersion&quot;: &quot;go1.1.2&quot;,<br />
&nbsp; &nbsp; &quot;Deps&quot;: [<br />
&nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;ImportPath&quot;: &quot;github.com/goinggo/utilities/workpool&quot;,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;Rev&quot;: &quot;7e6141d61b2a16ae83988907308f8e09f703a0d0&quot;<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; ]<br />
}
</div>
<br />
The Godeps file contains everything <b>godep</b> needs to create a reproducible build. The Godep file lists each 3rd party package and the git commit number for that version of code to use.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">

<a href="../../../images/goinggo/Screen+Shot+2013-10-26+at+11.12.44+AM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;" target="_blank"><span class="sr-only">View linked image: Screen Shot</span>![Screen Shot](../../../images/goinggo/Screen+Shot+2013-10-26+at+11.12.44+AM.png)</a>

</div>
<br />
Now remove the 3rd party package from its original location and perform a build using the <b>godep</b> tool:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
godep go build
</div>
<br />
<div class="separator" style="clear: both; text-align: center;">

<a href="../../../images/goinggo/Screen+Shot+2013-10-26+at+11.16.25+AM.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;" target="_blank"><span class="sr-only">View linked image: Screen Shot</span>![Screen Shot](../../../images/goinggo/Screen+Shot+2013-10-26+at+11.16.25+AM.png)</a>

</div>
<br />
If you remove the original 3rd party code, you can't use 'go build' directly anymore, the imports don't exist. To build the project use 'go build' from the&nbsp;<b>godep</b>&nbsp;tool.<br />
<br />
You can also use 'go install' and 'go test' as long as you run those commands through <b>godep</b>.<br />
<br />
godep go build<br />
godep go install<br />
godep go test<br />
<br />
<div style="clear: both;">
</div>
To make this work,&nbsp;<b>godep</b>&nbsp;performs a bit of magic. It uses a working directory and manipulates the GOPATH underneath.<br />
<br />
Run the <b>godep path</b> command from inside the project folder:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
cd $GOPATH/src/github.com/goinggo/newssearch<br />
godep path</div>
<br />
You should see the following output:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
/var/folders/8q/d2pfdk_x4qd4__l6gypvzsw40000gn/T/godep/rev/<b>7e/6141d61b2a16ae83988907308f8e09f703a0d0
</b></div>
<br />
If you open that folder you will see the code for that version. This code is being used to build the project:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">

<a href="../../../images/goinggo/Screen+Shot+2013-10-26+at+7.18.42+PM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;" target="_blank"><span class="sr-only">View linked image: Screen Shot</span>![Screen Shot](../../../images/goinggo/Screen+Shot+2013-10-26+at+7.18.42+PM.png)</a>

</div>
<br />
<div class="separator" style="clear: both; text-align: center;">
</div>
The <b>godep</b> tool will continue to use the code from this location to perform future builds if it exists. Calling <b>godep go build</b>&nbsp;will&nbsp;download the version of code specified in the Godeps file if it doesn't already exist.<br />
<br />
If you open any of your source code files you will see the imports have not changed. The way <b>godep</b> works, it doesn't have to change a thing. This is one of the really awesome things about <b>godep</b>.<br />
<br />
<h3>
<b>Updating Dependencies</b></h3>
When it is time to update one of your 3rd party libraries just 'go get' it. Then you just need to run <b>godep save</b> once again to update the Godeps file. Because the imports paths in the source code files are not changed,&nbsp;<b>godep</b>&nbsp;will find and update the dependencies.<br />
<br />
I have changed the 3rd party package and pushed it up to GitHub:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">

<a href="../../../images/goinggo/Screen+Shot+2013-10-26+at+7.03.43+PM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;" target="_blank"><span class="sr-only">View linked image: Screen Shot</span>![Screen Shot](../../../images/goinggo/Screen+Shot+2013-10-26+at+7.03.43+PM.png)</a>

</div>
<br />
Now I 'go get' the code changes and update the Godeps file:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
go get github.com/goinggo/utilities<br />
cd $GOPATH/src/github.com/goinggo/newssearch<br />
godep save
</div>
<br />
If I open the Godeps file the dependencies have changed:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
{<br />
&nbsp; &nbsp; &quot;ImportPath&quot;: &quot;github.com/goinggo/newssearch&quot;,<br />
&nbsp; &nbsp; &quot;GoVersion&quot;: &quot;go1.1.2&quot;,<br />
&nbsp; &nbsp; &quot;Deps&quot;: [<br />
&nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;ImportPath&quot;: &quot;github.com/goinggo/utilities/workpool&quot;,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &quot;Rev&quot;: &quot;8ecd01ec035e29915aa6897a3385ee4f8d80cc05&quot;<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; ]<br />
}
</div>
<br />
Now I use <b>godep</b> to build the code:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
godep go build</div>
<br />
<div class="separator" style="clear: both; text-align: center;">

<a href="../../../images/goinggo/Screen+Shot+2013-10-26+at+7.14.57+PM.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;" target="_blank"><span class="sr-only">View linked image: Screen Shot</span>![Screen Shot](../../../images/goinggo/Screen+Shot+2013-10-26+at+7.14.57+PM.png)</a>

</div>
<br />
The <b>godep</b> tool downloaded the new version and built the code successfully.<br />
<br />
<div style="clear: both;">
</div>
<h3>
<b>Vendoring</b></h3>
Vendoring is the act of making your own copy of the 3rd party packages your project is using. Those copies are traditionally placed inside each project and then saved in the project repository. The <b>godep</b> tool supports Vendoring and will place the copies inside the project that are using them.<br />
<br />
To Vendor code with <b>godep,</b>&nbsp;don't use any options with the save command. First clean the workspace and download a new version of the News Search program:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
export GOPATH=$HOME/example<br />
go get github.com/goinggo/newssearch<br />
cd $GOPATH/src/github.com/goinggo/newssearch
</div>
<br />
Now issue the <b>godep save</b> command again but this time without the copy option:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
godep save</div>
<br />
<div class="separator" style="clear: both; text-align: center;">

<a href="../../../images/goinggo/Screen+Shot+2013-10-26+at+7.35.04+PM.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;" target="_blank"><span class="sr-only">View linked image: Screen Shot</span>![Screen Shot](../../../images/goinggo/Screen+Shot+2013-10-26+at+7.35.04+PM.png)</a>

</div>
<br />
This time you will have a Godeps folder with a special workspace subfolder and the Godeps file. All the 3rd party packages are copied into the workspace folder under src. This is setup to work with GOPATH.<br />
<br />
Version control files are removed and no import paths are changed in any of the source code files.<br />
<div style="clear: both;">
</div>
Next remove the original code for the 3rd party package and perform the build:<br />
<br />
<div style="background-color: #efefef; font-family: 'Courier New', Courier, monospace; font-size: small; overflow-x: scroll; padding: 10px; white-space: nowrap;">
godep go build
</div>
<br />
The build is successful and everything is ready to be pushed back into the repository.<br />
<br />
Performing an update is as simple as downloading the new version of the 3rd party package and running <b>godep save</b>&nbsp;again.<br />
<br />
<h3>
<b>Conclusion</b></h3>
The <b>godep</b> tool solves many of the problems that exist with creating reproducible builds. It is incredibly easy to use and sits on top of the go tooling. It doesn't change anything about go, how you write go programs or how you import 3rd party packages. The only drawback is that&nbsp;<b>godep</b>&nbsp;does not&nbsp;support Bazaar using the non-vendored option.<br />
<br />
For the public packages your are publishing, you can include a Godeps file to provide your &quot;stable build&quot; configuration. Package users can choose to use it or not. That is really cool. Build the code with go directly or through <b>godep</b>.<br />
<br />
In the end,&nbsp;<b>godep</b> is a tool that:<br />
<br />
1. Supports a Vendor and Non-Vendor solution that provides a reproducible build<br />
2. Maintains backwards compatible with all existing Go packages<br />
3. Provides a way to publish and access the &quot;stable build&quot; configuration of a product<br />
4. Easy to update package dependencies when new package versions are available
