Go Vendoring Beginner TutorialMar 29, 2016 · 4 minute read · Comments
After the NPM problems that our friends in the NodeJS World experienced, I became more interested in how Go handled dependancies. If you have spent a bit of time in Go you no doubt will say: “Hey we just use the GOPATH and put it all in there, all’s well. I have also heard many times that people just create one GOPATH per project (I did this too). If you use One GOPATH like most of us do you may eventually encouter problems (i.e.: What if project A depends on import X version 1 and another project B deends on the same inport but version 2. You can’t use 2 GOPATHs or the compiler will get comfused may or may not give you the right version in your builds, so what to do?
Enter Go Vendoring
Method 1 (Godep)
I believe this was the first original vendoring tool for golang (I am sure some of you will correct me if I am wrong) and it is still quite popular. It’s simple to install and use but I was told that it does have some problems with dependencies of your dependencies. But if you are deploying a project to Heroku, Godeps is the only vendoring tool supported. Give it a try here is a simple step-by-step to get you started.
- Install godep. ‘go get github.com/tools/godep’
- From your project directory, Run
‘godep save’ - this will create 2 directories – One for it’s config files, and;
– the ‘vendor’ directory for the dependancies
- After running the precious command you can check vendor folder. You’d see all dependencies.
- Build/Run your project as usual but add the -v to the go command if you want
to see it display the imports that it is using.
‘go run -v main.go’ (or) ‘go build -v’
Method 2 (govendor)
Many people are now using govendor as their main vendoring tool. govendor will flatten out all the dependencies in your project as well as import dependencies from your gopath or like ‘go get’. Here is a quick setup for a project
- Install govendor. ‘go get -u github.com/kardianos/govendor’
- Go to your project directory (it must be in the GOPATH src)
- Initialize the project for go vendor
- Create your dependencies (this will download from github or other if required).
‘govendor add +external’ - This will copy all your dependencies from your GOPATH to the vendor directory - There are several more options for the add command and I suggest you check them out in the README at the github govendor project
- After you have done that you can list your dependencies using the ‘govendor list’ and update them as well with ‘govendor update’. Also note that the ‘govendor fetch’ command will get a dependency from an external source and place it in the vendor directory.
- Build/Run your project as usual but add the -v to the go command if you want to see it display
the imports that it is using.
‘go run -v main.go’ (or) ‘go build -v’
Note: one more thing if you do not wish to store your dependencies in your github project make sure your .gitignore contains the line: ‘/vendor/**/.git’ – this will ignore all the dependencies for git purposes but leave the govendor configs intact so you can use ‘govendor sync’ after you clone the project in a new location.
Method 3 (etc,…)
There are many tools to achieve the results above I am going to stop here because I could continue for a long
time and I did call this a short beginner go vendoring tutorial (enphasis on “short”). Here is a pretty good list
of all of them go check some out and let me know what you think (if there is a better one than the 2 above - I’d
be curious to hear from you)
In this quick beginner’s tutorial I briefly introduced you to why we use vendoring, when to use it and I showed
2 step by step methods using 2 separate tools to achive the same results. Have fun vendoring and remember to come
checkout Part 3 of Writing a Text Adventure in Go later this week.
Go with the Gopher!!!
*** Sign up for my email list to keep in touch with all the interesting new happenings in the go community with the GolangNewsFeed