Use govendor to implement vendoring

The meaning of vendoring in Go is squeezing a project’s all dependencies into its vendor directory. Since Go 1.6, if there is a vendor directory in current package or its parent’s directory, the dependency will be searched in vendor directory first. Govendor is such a tool to help you make use of the vendor feature. In the following example, I will demonstrate how to use govendor step by step:

(1) To be more clear, I clean $GOPATH folder first:

  1. # tree
  2. .
  3. 0 directories, 0 files

(2) I still use playstack project to do a demo, download it:

  1. # go get github.com/NanXiao/playstack/play
  2. # tree
  3. .
  4. ├── bin
  5. └── play
  6. ├── pkg
  7. └── linux_amd64
  8. └── github.com
  9. └── NanXiao
  10. └── stack.a
  11. └── src
  12. └── github.com
  13. └── NanXiao
  14. ├── playstack
  15. ├── LICENSE
  16. └── play
  17. └── main.go
  18. └── stack
  19. ├── LICENSE
  20. ├── README.md
  21. ├── stack.go
  22. └── stack_test.go
  23. 11 directories, 8 files

The playstack depends on another 3rd-party package: stack.

(3) Install govendor:

  1. # go get -u github.com/kardianos/govendor

(4) Change to playstack directory, and run “govendor init“ command:

  1. # cd src/github.com/NanXiao/playstack/
  2. # govendor init
  3. # tree
  4. .
  5. ├── LICENSE
  6. ├── play
  7. └── main.go
  8. └── vendor
  9. └── vendor.json
  10. 2 directories, 3 files

You can see there is an additional vendor folder which contains vendor.json file:

  1. # cat vendor/vendor.json
  2. {
  3. "comment": "",
  4. "ignore": "test",
  5. "package": [],
  6. "rootPath": "github.com/NanXiao/playstack"
  7. }

(5) Execute “govendor add +external“ command:

  1. # govendor add +external
  2. # tree
  3. .
  4. ├── LICENSE
  5. ├── play
  6. └── main.go
  7. └── vendor
  8. ├── github.com
  9. └── NanXiao
  10. └── stack
  11. ├── LICENSE
  12. ├── README.md
  13. └── stack.go
  14. └── vendor.json

Yeah, the stack project is copied to vendor directory now. Look at vendor/vendor.json file again:

  1. # cat vendor/vendor.json
  2. {
  3. "comment": "",
  4. "ignore": "test",
  5. "package": [
  6. {
  7. "checksumSHA1": "3v5ClsvqF5lU/3E3c+1gf/zVeK0=",
  8. "path": "github.com/NanXiao/stack",
  9. "revision": "bfb214dbdb387d1c561b3b6f305ee0d8444c864b",
  10. "revisionTime": "2016-04-01T05:28:44Z"
  11. }
  12. ],
  13. "rootPath": "github.com/NanXiao/playstack"
  14. }

The stack package info has been updated in vendor/vendor.json file.

Notice: “govendor add“ copies packages from $GOPATH, and you can use “govendor fetch“ to download packages from network. You can verify it through removing stack package in $GOPATH, and execute “govendor fetch github.com/NanXiao/stack“ command.

(6) Update playstack in github:

image

This time, clean $GOPATH folder and run “go get github.com/NanXiao/playstack/play“ again:

  1. # go get github.com/NanXiao/playstack/play
  2. # tree
  3. .
  4. ├── bin
  5. └── play
  6. ├── pkg
  7. └── linux_amd64
  8. └── github.com
  9. └── NanXiao
  10. └── playstack
  11. └── vendor
  12. └── github.com
  13. └── NanXiao
  14. └── stack.a
  15. └── src
  16. └── github.com
  17. └── NanXiao
  18. └── playstack
  19. ├── LICENSE
  20. ├── play
  21. └── main.go
  22. └── vendor
  23. ├── github.com
  24. └── NanXiao
  25. └── stack
  26. ├── LICENSE
  27. ├── README.md
  28. └── stack.go
  29. └── vendor.json
  30. 18 directories, 8 files

Compared to previous case, it is no need to store stack in $GOPATH/src/github.com/NanXiao directory, since playstack has embedded it in its vendor folder.

This is just a simple intro of govendor, for more commands’ usages, you should visit its project home page.

Reference:
What does the term “vendoring” or “to vendor” mean for Ruby on Rails?;
Understanding and using the vendor folder;
Go Vendoring Beginner Tutorial.