安装govendor
参考Go Dependencies via govendor
English
使用go 1.18,在任意目录执行go install
安装govendor
go install github.com/kardianos/govendor@latest
将项目放置到$GOPATH/src
1,在项目内列出依赖列表
govendor list
可以看到该项目内的各种依赖
...
l moby/volume
l moby/volume/drivers
l moby/volume/local
l moby/volume/mounts
l moby/volume/service
l moby/volume/service/opts
l moby/volume/testutils
m appengine
m appengine_internal
m appengine_internal/base
之后对每个没下载的依赖执行fetch即可:
govendor fetch github.com/docker/docker/pkg/reexec
govendor fetch github.com/docker/docker/rootless
...
不过用fetch的做法有点低效,笔者还没找到其他高效做法
踩坑
下载govendor失败
a connection attempt failed because the connected party did not properly respond after period of tim
GO A connection attempt failed because the connected party did not properly respond after a period…
如果go install
下载失败,需要修改go的代理为国内镜像
go env -w GOPROXY=https://goproxy.cn
govendor路径错误
govendor error package not a go package or not in gopath
你的项目没有放置在$GOPATH/src
。
首先执行go env GOPATH
查看路径,如果是不符合预期的,就参考windows11 如何修改环境变量,修改GOPATH系统变量。
删除govendor依赖
使用govendor fetch github.com/spf13/cobra
来下载依赖,并到$GOPATH/.cache
下删除依赖。
拉取不到指定分支
执行了govendor fetch命令后, 报错如下
D:\codes\src\moby>govendor fetch github.com/spf13/cobra
# cd D:\codes\.cache\govendor\github.com\spf13\cobra; git reset --hard origin/master
fatal: ambiguous argument 'origin/master': unknown revision or path not in the working tree
.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
Error: failed to sync to latest revision exit status 128
Failed to fetch package "github.com/spf13/cobra"
可以看到,govendor尝试执行git reset --hard origin/master
,但找不到这个分支,因为原仓库本来就没这个分支
所以改为运行后,执行成功
govendor fetch github.com/spf13/cobra@main
参考Go Vendoring Beginner Tutorial的相关说明,提到
Go to your project directory (it must be in the GOPATH src)
可知,项目必须在GOPATH的src目录下。 ↩︎