Skip to content

Commit b4631f0

Browse files
committed
Add use-govendor-to-implement-vendoring.md
1 parent 18597a7 commit b4631f0

File tree

3 files changed

+151
-2
lines changed

3 files changed

+151
-2
lines changed

SUMMARY.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
* [Create Go workspace](posts/create-go-workspace.md)
44
* [Package](posts/package.md)
55
* [“go build” and “go install”](posts/go-build-vs-go-install.md)
6-
* ["go get" command](posts/go-get-command.md)
6+
* ["go get" command](posts/go-get-command.md)
7+
* [Use govendor to implement vendoring](post/use-govendor-to-implement-vendoring.md)
78
* [init function](posts/init-function.md)
89
* [Short variable declaration](posts/short-variable-declarations.md)
910
* [“nil slice” vs “nil map”](posts/nil-slice-vs-nil-map.md)
@@ -14,7 +15,7 @@
1415
* [Two-dimensional slice](posts/two-dimensional-slice.md)
1516
* [Reallocating underlying array of slice](posts/reallocating-underlying-array-of-slice.md)
1617
* [copy](posts/copy.md)
17-
* [Array](posts/array.md)
18+
* [Array](posts/array.md)
1819
* [Conversion between array and slice](posts/conversion-between-array-and-slice.md)
1920
* [Accessing map](posts/accessing-map.md)
2021
* [switch](posts/switch.md)

images/govendor-playstack.JPG

34.1 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
Use govendor to implement vendoring
2+
----
3+
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](https://github.com/kardianos/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:
4+
5+
(1) To be more clear, I clean `$GOPATH` folder first:
6+
7+
# tree
8+
.
9+
10+
0 directories, 0 files
11+
(2) I still use [playstack](https://github.com/NanXiao/playstack) project to do a demo, download it:
12+
13+
# go get github.com/NanXiao/playstack/play
14+
# tree
15+
.
16+
├── bin
17+
│   └── play
18+
├── pkg
19+
│   └── linux_amd64
20+
│   └── github.com
21+
│   └── NanXiao
22+
│   └── stack.a
23+
└── src
24+
└── github.com
25+
└── NanXiao
26+
├── playstack
27+
│   ├── LICENSE
28+
│   └── play
29+
│   └── main.go
30+
└── stack
31+
├── LICENSE
32+
├── README.md
33+
├── stack.go
34+
└── stack_test.go
35+
36+
11 directories, 8 files
37+
The `playstack` depends on another 3rd-party package: [stack](https://github.com/NanXiao/stack).
38+
39+
(3) Install `govendor`:
40+
41+
# go get -u github.com/kardianos/govendor
42+
43+
(4) Change to `playstack` directory, and run "`govendor init`" command:
44+
45+
# cd src/github.com/NanXiao/playstack/
46+
# govendor init
47+
# tree
48+
.
49+
├── LICENSE
50+
├── play
51+
│   └── main.go
52+
└── vendor
53+
└── vendor.json
54+
55+
2 directories, 3 files
56+
You can see there is an additional `vendor` folder which contains `vendor.json` file:
57+
58+
# cat vendor/vendor.json
59+
{
60+
"comment": "",
61+
"ignore": "test",
62+
"package": [],
63+
"rootPath": "github.com/NanXiao/playstack"
64+
}
65+
66+
(5) Execute "`govendor add +external`" command:
67+
68+
# govendor add +external
69+
# tree
70+
.
71+
├── LICENSE
72+
├── play
73+
│   └── main.go
74+
└── vendor
75+
├── github.com
76+
│   └── NanXiao
77+
│   └── stack
78+
│   ├── LICENSE
79+
│   ├── README.md
80+
│   └── stack.go
81+
└── vendor.json
82+
83+
Yeah, the `stack` project is copied to `vendor` directory now. Look at `vendor/vendor.json` file again:
84+
85+
# cat vendor/vendor.json
86+
{
87+
"comment": "",
88+
"ignore": "test",
89+
"package": [
90+
{
91+
"checksumSHA1": "3v5ClsvqF5lU/3E3c+1gf/zVeK0=",
92+
"path": "github.com/NanXiao/stack",
93+
"revision": "bfb214dbdb387d1c561b3b6f305ee0d8444c864b",
94+
"revisionTime": "2016-04-01T05:28:44Z"
95+
}
96+
],
97+
"rootPath": "github.com/NanXiao/playstack"
98+
}
99+
The `stack` package info has been updated in `vendor/vendor.json` file.
100+
101+
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.
102+
103+
(6) Update `playstack` in `github`:
104+
105+
![image](https://raw.githubusercontent.com/NanXiao/golang-101-Hacks/master/images/govendor-playstack.JPG)
106+
107+
This time, clean `$GOPATH` folder and run "`go get github.com/NanXiao/playstack/play`" again:
108+
109+
# go get github.com/NanXiao/playstack/play
110+
# tree
111+
.
112+
├── bin
113+
│   └── play
114+
├── pkg
115+
│   └── linux_amd64
116+
│   └── github.com
117+
│   └── NanXiao
118+
│   └── playstack
119+
│   └── vendor
120+
│   └── github.com
121+
│   └── NanXiao
122+
│   └── stack.a
123+
└── src
124+
└── github.com
125+
└── NanXiao
126+
└── playstack
127+
├── LICENSE
128+
├── play
129+
│   └── main.go
130+
└── vendor
131+
├── github.com
132+
│   └── NanXiao
133+
│   └── stack
134+
│   ├── LICENSE
135+
│   ├── README.md
136+
│   └── stack.go
137+
└── vendor.json
138+
139+
18 directories, 8 files
140+
141+
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.
142+
143+
This is just a simple intro of `govendor`, for more commands' usages, you should visit its project [home page](https://github.com/kardianos/govendor).
144+
145+
Reference:
146+
[What does the term “vendoring” or “to vendor” mean for Ruby on Rails?](http://stackoverflow.com/questions/11378921/what-does-the-term-vendoring-or-to-vendor-mean-for-ruby-on-rails);
147+
[Understanding and using the vendor folder](https://blog.gopheracademy.com/advent-2015/vendor-folder/);
148+
[Go Vendoring Beginner Tutorial](https://gocodecloud.com/blog/2016/03/29/go-vendoring-beginner-tutorial/).

0 commit comments

Comments
 (0)