一、安装govendor
1、下载并编译
go get -u -x -v github.com/kardianos/govendor
2、验证一下是否能用:
govendor --version
//v1.0.9
如果命令找不到,先确认一下$GOPATH/bin
目录是否有govendor
这个文件,然后再看看$GOPATH/bin
有没有加入到PATH
全局变量中。
二、克隆项目
1、本文以Data-register项目为例。
git clone git@git.ju3ban.net:artarva/data-register.git
2、切换到项目目录下:
cd data-register
三、使用govendor
1、如果是首次使用govendor,我们需要先初始化一下:
govendor init
执行后会生成一个vendor.json, 里面并没有依赖包信息。
2、查看项目中所有在使用的包:
govendor list
如下:
e golang.org/x/crypto/pbkdf2
e golang.org/x/crypto/scrypt
pl data-register
l data-register/api/v2
l data-register/config
l data-register/library
l data-register/library/app
l data-register/library/bpl
l data-register/library/cmd
l data-register/library/contract/abi
l data-register/library/contract/msgpack
l data-register/library/crypto-go/crypto
l data-register/library/crypto-go/crypto/aes
l data-register/library/crypto-go/crypto/secp256k1
l data-register/library/e
l data-register/library/file
l data-register/library/hash
l data-register/library/helper
l data-register/library/logging
l data-register/library/safemath
l data-register/model
l data-register/router
m github.com/cihub/seelog
m github.com/cihub/seelog/archive
m github.com/cihub/seelog/archive/gzip
m github.com/cihub/seelog/archive/tar
m github.com/cihub/seelog/archive/zip
m github.com/davecgh/go-spew/spew
m github.com/gin-contrib/sse
m github.com/gin-gonic/gin
m github.com/gin-gonic/gin/binding
m github.com/gin-gonic/gin/internal/json
m github.com/gin-gonic/gin/render
m github.com/golang/protobuf/proto
m github.com/google/uuid
m github.com/json-iterator/go
m github.com/konsorten/go-windows-terminal-sequences
m github.com/mattn/go-isatty
m github.com/modern-go/concurrent
m github.com/modern-go/reflect2
m github.com/naoina/go-stringutil
m github.com/naoina/toml
m github.com/naoina/toml/ast
m github.com/pborman/uuid
m github.com/pmezard/go-difflib/difflib
m github.com/sirupsen/logrus
m github.com/stretchr/testify/assert
m github.com/ugorji/go/codec
m gopkg.in/go-playground/validator.v8
m gopkg.in/mgo.v2
m gopkg.in/mgo.v2/bson
m gopkg.in/mgo.v2/internal/json
m gopkg.in/mgo.v2/internal/sasl
m gopkg.in/mgo.v2/internal/scram
m gopkg.in/urfave/cli.v2
m gopkg.in/yaml.v2
e github.com/cihub/seelog
e github.com/cihub/seelog/archive
e github.com/cihub/seelog/archive/gzip
e github.com/cihub/seelog/archive/tar
e github.com/cihub/seelog/archive/zip
e github.com/gin-contrib/sse
e github.com/gin-gonic/gin
e github.com/gin-gonic/gin/binding
e github.com/gin-gonic/gin/internal/json
e github.com/gin-gonic/gin/render
e github.com/golang/protobuf/proto
e github.com/google/uuid
e github.com/json-iterator/go
e github.com/konsorten/go-windows-terminal-sequences
e github.com/mattn/go-isatty
e github.com/modern-go/concurrent
e github.com/modern-go/reflect2
e github.com/naoina/go-stringutil
e github.com/naoina/toml
e github.com/naoina/toml/ast
e github.com/pborman/uuid
e github.com/sirupsen/logrus
e github.com/stretchr/testify/assert
e github.com/davecgh/go-spew/spew
e github.com/pmezard/go-difflib/difflib
e github.com/ugorji/go/codec
e golang.org/x/crypto/pbkdf2
e golang.org/x/crypto/scrypt
e golang.org/x/sys/unix
e gopkg.in/go-playground/validator.v8
e gopkg.in/mgo.v2
e gopkg.in/mgo.v2/bson
e gopkg.in/mgo.v2/internal/json
e gopkg.in/mgo.v2/internal/sasl
e gopkg.in/mgo.v2/internal/scram
e gopkg.in/urfave/cli.v2
e gopkg.in/yaml.v2
pl data-register
l data-register/api/v2
l data-register/config
l data-register/library
l data-register/library/app
l data-register/library/bpl
l data-register/library/cmd
l data-register/library/contract/abi
l data-register/library/contract/msgpack
l data-register/library/crypto-go/crypto
l data-register/library/crypto-go/crypto/aes
l data-register/library/crypto-go/crypto/secp256k1
l data-register/library/e
l data-register/library/file
l data-register/library/hash
l data-register/library/helper
l data-register/library/logging
l data-register/library/safemath
l data-register/model
l data-register/router
m github.com/davecgh/go-spew/spew
m github.com/pmezard/go-difflib/difflib
以上每一行前面的字母简写代表包所处的位置(本地还是远程)和状态,可以对照如下表格来搞清楚:哪些包是需要下载安装的、哪些包是已经被govendor管理了的。
状态 | 缩写状态 | 含义 |
---|---|---|
+local | l | 本地包,即项目自身的包组织 |
+external | e | 外部包,即被$GOPATH管理,但不在vendor目录下 |
+vendor | v | 已被govendor管理,即在vendor目录下 |
+std | s | 标准库中的包 |
+unused | u | 未使用的包,即包在vendor目录下,但项目并没有用到 |
+missing | m | 代码引用了依赖包,但该包并没有找到 |
+program | p | 主程序包,意味着可以编译为执行文件 |
+outside | 外部包和缺失的包 | |
+all | 所有的包 |
另外,pl表示可以编译成可执行程序(即主程序)。
注意:下载
golang.org/x
下的Go语言官方包时常速度很慢或者下载不下来,就需要我们手工去git clone
包了,这里整理了一个批量下载的脚本:golang.org/x一键安装脚本,复制保存到~/go/src/x.sh
(路径可任意)然后执行即可。
3、用govendor来管理包的思路是,先将远程包通过go get 库资源路径
命令下载到本地,然后执行:
govendor add +external
将包信息整理到vendor.json文件中,此时再去查看vendor.json文件时,里面就记录了项目中要使用的包的信息:
在下次go build
编译时,如果依赖包没有找到,govendor会自动按vendor.json里指定的资源路径去下载安装。
4、我们需要将这个vendor.json添加到git, 但忽略vendor下的其他文件.
.gitignore
/vendor/*
!/vendor/vendor.json
5、拉取依赖到vendor
govendor sync
其他人可以使用vendor.json重新安装依赖包到vendor目录。
注意:如果GOPAHT设置了多个目录时,vendor所在的目录一定要在第一个GOPAHT下。
参考:
https://www.jianshu.com/p/88669ba57d04list
有疑问加站长微信联系(非本文作者)