Go Package Management

dep / Govendor / Godep

dep

https://github.com/golang/dep
https://github.com/golang/dep/blob/master/docs/FAQ.md#does-dep-replace-go-get

dep vs. go get

go get: I want to download the source code for a go project so that I can work on it myself, or to install a tool. This clones the repo under GOPATH for all to use.

dep ensure:I have imported a new dependency in my code and want to download the dependency so I can start using it. My workflow is “add the import to the code, and then run dep ensure so that the manifest/lock/vendor are updated”. This clones the repo under my project’s vendor directory, and remembers the revision used so that everyone who works on my project is guaranteed to be using the same version of dependencies.

Install

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

Use

yzw@yzw-vm:~/go/src/hello$ ls
hello.go

yzw@yzw-vm:~/go/src/hello$ dep init
  Using ^3.0.0 as constraint for direct dep rsc.io/quote
  Locking in v3.0.0 (d88915d) for direct dep rsc.io/quote
  Locking in v1.99.99 (732a3c4) for transitive dep rsc.io/sampler
  Locking in v0.3.0 (f21a4df) for transitive dep golang.org/x/text

yzw@yzw-vm:~/go/src/hello$ ls
Gopkg.lock  Gopkg.toml  hello.go  vendor
yzw@yzw-vm:~/go/src/hello$ ls vendor/
golang.org  rsc.io

yzw@yzw-vm:~/go/src/hello$ cat Gopkg.toml 
...
[[constraint]]
  name = "rsc.io/quote"
  version = "3.0.0"

[prune]
  go-tests = true
  unused-packages = true

yzw@yzw-vm:~/go/src/hello$ cat Gopkg.lock 
# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.


[[projects]]
  digest = "1:17ad89bd57485e11f306d41b703f53089fb0803682707b21fe9b3b29467d37a0"
  name = "golang.org/x/text"
  packages = [
    "internal/gen",
    "internal/tag",
    "language",
    "unicode/cldr",
  ]
  pruneopts = "UT"
  revision = "f21a4dfb5e38f5895301dc265a8def02365cc3d0"
  version = "v0.3.0"

[[projects]]
  digest = "1:5d1998b0bc9ea3fd2c2afacb273e5793506acc9241eef5598cfa1945f9518932"
  name = "rsc.io/quote"
  packages = ["."]
  pruneopts = "UT"
  revision = "d88915d7e77ed0fd35d0a022a2f244e2202fd8c8"
  version = "v3.0.0"

[[projects]]
  digest = "1:967a8a854773a41db29413f05f14bb9acac7b31342f5dbd90af861d90fb7864d"
  name = "rsc.io/sampler"
  packages = ["."]
  pruneopts = "UT"
  revision = "732a3c400797d8835f2af34a9561f155bef85435"
  version = "v1.99.99"

[solve-meta]
  analyzer-name = "dep"
  analyzer-version = 1
  input-imports = ["rsc.io/quote"]
  solver-name = "gps-cdcl"
  solver-version = 1

yzw@yzw-vm:~/go/src/hello$ dep status
PROJECT            CONSTRAINT  VERSION   REVISION  LATEST    PKGS USED
golang.org/x/text  v0.3.0      v0.3.0    f21a4df   v0.3.0    4  
rsc.io/quote       ^3.0.0      v3.0.0    d88915d   v3.0.0    1  
rsc.io/sampler     v1.99.99    v1.99.99  732a3c4   v1.99.99  1  

other usages

$ dep ensure -add github.com/xx/xx
$ dep ensure -update
$ dep install

Need to run dep ensure when Gopkg.toml and Gopkg.lock are out of sync.

This ensures that any of your project’s dependencies are included in the lock file and vendor directory.

Gopkg.toml Gopkg.lock vendor

dep cache

yzw@yzw-vm:~/go/pkg/dep/sources$ ls
https---github.com-rsc-quote
https---github.com-rsc-sampler

Godep

https://github.com/tools/godep

NOTE:

The Go community now has the dep project to manage dependencies. Please consider trying to migrate from Godep to dep. If there is an issue preventing you from migrating please file an issue with dep so the problem can be corrected. Godep will continue to be supported for some time but is considered to be in a state of support rather than active feature development.

Install

$ go get github.com/tools/godep

Use

# add single package to the project
$ go get -u -v github.com/spf13/cobra
$ godep save "github.com/spf13/cobra"

# add all imported packages to to the vendor dir
$ godep save

# add multi packages to the project
$ godep ./...

# restore packages according Godeps/Godeps.json
$ godep restore

Govendor

https://github.com/kardianos/govendor

Install

$ go get -u github.com/kardianos/govendor

Use

yzw@yzw-vm:~/go/src/hello$ ls
hello.go

yzw@yzw-vm:~/go/src/hello$ govendor init
yzw@yzw-vm:~/go/src/hello$ ls
hello.go  vendor
yzw@yzw-vm:~/go/src/hello$ ls vendor/
vendor.json

yzw@yzw-vm:~/go/src/hello$ govendor list
pl  hello           
  m rsc.io/quote   

yzw@yzw-vm:~/go/src/hello$ cat vendor/vendor.json 
{
	"comment": "",
	"ignore": "test",
	"package": [],
	"rootPath": "hello"
}

yzw@yzw-vm:~/go/src/hello$ go get -u -v rsc.io/quote
...
yzw@yzw-vm:~/go/src/hello$ govendor add +external

yzw@yzw-vm:~/go/src/hello$ ls vendor/
rsc.io  vendor.json
yzw@yzw-vm:~/go/src/hello$ cat vendor/vendor.json 
{
	"comment": "",
	"ignore": "test",
	"package": [
		{
			"checksumSHA1": "w+/nmJ42g1lDdCOeZ6l9pRNYWDI=",
			"path": "rsc.io/quote",
			"revision": "5d9f230bcfbae514bb6c2215694c2ce7273fc604",
			"revisionTime": "2018-07-10T14:47:37Z"
		},
		{
			"checksumSHA1": "dmtjMQ4RDoIU/YAyEi8qzMrqwJc=",
			"path": "rsc.io/quote/v3",
			"revision": "5d9f230bcfbae514bb6c2215694c2ce7273fc604",
			"revisionTime": "2018-07-10T14:47:37Z"
		}
	],
	"rootPath": "hello"
}

golang 

See also

comments powered by Disqus