Golang Vendoring How to add vendoring to an existing Go project
How to add Vendoring to an existing Go Project to manage your dependencies
In this post I want to discuss how you can add vendoring for your dependencies to an existing Go project that does not currently have a vendor directory.
I am working on a new version of Best Food Near Me. The original version used a version of Go that did not have vendoring support, but it has since been upgraded. Some of my dependencies had changed, and things did not work the same. I decided it would be prudent to add vendoring to my new version to better track changes in my dependencies.
There are a number of tools that can help make use of the vendor feature easier. I tried a number of them such as GoDep and GoVendor but they did not work as well against an existing project. If you are starting a new project from scratch, I am sure they would be a better fit.
I settled on using Glide as the tool to help me manage the dependencies in my vendor directory of my Go project. Here are the steps I took
-
Install Glide curl https://glide.sh/get | sh
-
Inside you project directory make a vendor directory
-
copy one dependency into the vendor directory from GOPATH
-
run “glide create” inside your project directory and choose semantic versioning and Minor patches when it prompts you.
-
You should now have a glide yaml file at this point. Now for each dependency not in your vendor subdirectory run glide get github.com/foo/bar for each replacing foo and bar with the actual paths for that dependency
There is a chance that some of your dependencies do not have a git tag for a semantic version, that is ok, if there is a specific git commit hash you would like to pin to, you can edit the yaml file and add a version for a dependency and specify the full hash commit.
You can check which dependencies you have currently vendored with the list command
If you want to update all of your dependencies in the vendor directory you can use the command
I hope you have found this short post useful information. If you have, please share.