go mod vendor
is fairly limited in scope,
it will only include what's needed from your dependencies to build your main program.
So your dependencies tests and non go files all get trimmed out.
But sometimes, you want to keep some of those extra files,
like html or protobuf files.
Now with embed
in 1.16, there's a way to make those files required as part of the build.
To recap, usually only .go
files needed to build your main program
are included in vendor
.
1» exa -T
2.
3├── testrepo-dependency
4│ ├── go.mod
5│ ├── hello.proto
6│ ├── index.html
7│ ├── v.go
8│ └── v_test.go
9└── testrepo-main
10 ├── go.mod
11 ├── main.go
12 └── vendor
13 ├── go.seankhliao.com
14 │ └── testrepo-dependency
15 │ └── v.go
16 └── modules.txt
We can force files to be needed as part of a build by embedding them,
so in the above example in v.go
:
1import "embed"
2
3//go:embed *.html *.proto *.go
4var _ embed.FS
//go:embed *
won't work because .git/
exists
but can't be embedded as it's not in the scope of a module.
This results in:
1» exa -T
2.
3├── testrepo-418
4│ ├── go.mod
5│ ├── hello.proto
6│ ├── index.html
7│ ├── v.go
8│ └── v_test.go
9└── testrepo-419
10 ├── go.mod
11 ├── main.go
12 ├── testrepo-419
13 └── vendor
14 ├── go.seankhliao.com
15 │ └── testrepo-418
16 │ ├── hello.proto
17 │ ├── index.html
18 │ ├── v.go
19 │ └── v_test.go
20 └── modules.txt