Skip to content

Instantly share code, notes, and snippets.

@subfuzion
Last active July 25, 2024 03:38
Show Gist options
  • Save subfuzion/12342599e26f5094e4e2d08e9d4ad50d to your computer and use it in GitHub Desktop.
Save subfuzion/12342599e26f5094e4e2d08e9d4ad50d to your computer and use it in GitHub Desktop.
Concise guide to golang/dep

Get (or update) dep

$ go get -u github.com/golang/dep/cmd/dep

Initialize your project

dep will analyize your project, identify your dependencies, pick the highest compatible version for each dependency, generate a Gopkg.toml manifest and Gopkg.lock files (see here for background on the two file format), and install dependencies in vendor/.

$ dep init

Notes

If you are already using glide, godep, vndr, or govend, dep will convert the existing dependency management file. See this issue and this interface for implementing configuration importers.

If you have an existing vendor/ directory, it will be backed up to _vendor-TIMESTAMP/.

You can tell dep to resolve dependencies from GOPATH before resorting to network mode with dep init -gopath. See here for a bit more detail.

Usage

Installing dependencies

$ dep ensure

dep's solver regenerates Gopkg.lock if it detects any change in your code imports and/or Gopkg.toml. If this is the case, then the new Gopkg.lock file is used to completely rewrite vendor/ (at some point vednor verification will allow the contents of vendor/ to also be checked for consistency with Gopkg.lock, updating only what is necessary to make the contents current.

Note

This process may be slow for large projects, especially the first time as dependencies are cloned or when it hasn't been run in a while and there are many changesets to fetch. dep manages a cache at $GOPATH/pkg/dep to mitigate fetch overhead. See here for further details.

Adding a dependency

$ dep ensure -add github.com/foo/bar

dep will update your Gopkg.toml, Gopkg.lock, and vendor/.

Checking status of dependencies

Use this to detect a mismatch between the state of your project, such as imports that have not been added (and therefore are not in the generated Gopkg.lock file). Use this information to update your configuration with dep ensure -add.

$ dep status

Updating dependencies

dep will update your dependencies to the latest versions that satisify the constraints specified in Gopkg.toml.

$ dep ensure -update github.com/foo/bar github.com/another/project...

or just

$ dep ensure -update

Note

The latest version means the latest version in a semver range or if depending on a branch, the tip of the branch.

Removing dependencies

  1. Remove the relevant import package statements and usage in your code
  2. Remove the relevant [[constraint]] rules from Gopkg.toml
  3. Run dep ensure

Editing packages that are dependencies of your project

Your project may rely on dependencies that you wish to edit. These dependencies should not be updated directly under vendor/ since they can be overwritted by dep. Instead, volatile dependencies should be removed manually from under vendor/ and then placed in the appropriate location in your $GOPATH. When building, Go will first search your vendor/ directory, then it will search your $GOPATH.

If the package update has been pushed to its public repo, then simply run dep ensure -update as described in Changing Dependencies.

Links

GitHub

FAQ

dep semver

@hamshif
Copy link

hamshif commented Feb 12, 2018

As a user of python virtualenvwrapper I would recommend being able to create a virtual dependancy tree anywhere and make the project dependant on it

@YongliangLi
Copy link

helpful dock 👍

@pradykaushik
Copy link

Very helpful. Thanks

@ravihara
Copy link

ravihara commented Nov 2, 2018

Very informative. Thanks.

@MoatazBM
Copy link

Thanks. Is there an equivalent option to glide novendor (nv)?

@Deewai
Copy link

Deewai commented Jun 6, 2019

Very helpful. Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment