Go dependency management, or vendoring as we Gophers say, has a rather longish and slightly complicated history. With Go dep the community now seems to slowly (?) but surely converge on a standard. Since I need it anyway in one of the projects I contribute to I thought it makes sense to get some hands-on knowledge under my belt, in a sandboxed environment, before I apply it ‘for real’.

OK, let’s jump right into it: first, we have to install Go dep, which assumes a more recent version of Go installed (I’m using 1.8):

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

Next, we’ll need a program we want to vendor, so here’s a simple example using amongst other things the Kubernetes Go client to create a primitive version of a cluster shell (side note I’m using Minishift to set up and run Kubernetes):

The goal is to vendor the packages imported in line 7 to 10. So, in order to do this, I executed in the directory where the main.go file is located these commands:

$ dep init

$ dep ensure k8s.io/client-go@^2.0.0

$ dep ensure github.com/chzyer/readline@1.4

Note that the last two lines are not strictly required, since dep is smart enough to figure out the versions needed itself. In my case I needed to pin the client-go package to the 2.0.0 version to make sure it works against the Kubernetes version I’m using.

After that step, the directory layout looks like in the following:

.

├── Gopkg.lock

├── Gopkg.toml

├── main.go

└── vendor

├── cloud.google.com

├── github.com

├── golang.org

├── google.golang.org

├── gopkg.in

└── k8s.io