公司打算用glide管理第三方包,研究了一下他的使用方法,遇到一些坑,故记录一下。(环境 Darwin)
安装
go get github.com/Masterminds/glide
初始化
glide init(create)
问题:
工程目录:
src$echo $GOPATH
/Users/shaozhenyu/go
src$pwd
/Users/shaozhenyu/test/event/src
此时初始化的时候会把内部的包都放进去,显然这是不对的
[INFO]–> Found reference to decode
[INFO]–> Adding sub-package adview to decode
[INFO]–> Found reference to handle
[INFO]–> Found reference to utils/aes
[INFO]–> Found reference to utils/log/hooks/fluent
所以在init的时候需要把当前目录加入到GOPATH中,然后再初始化
/Users/shaozhenyu/go:/Users/shaozhenyu/test/event
可以看到只有第三方的包了
[INFO]Generating a YAML configuration file and guessing the dependencies
[INFO]Attempting to import from other package managers (use –skip-import to skip)
[INFO]Scanning code to look for dependencies
[INFO]–> Found reference to github.com/buaazp/fasthttprouter
[INFO]–> Found reference to github.com/tinylib/msgp/msgp
[INFO]–> Found reference to github.com/valyala/fasthttp
[INFO]–> Found reference to gopkg.in/redis.v5
[INFO]–> Found reference to gopkg.in/yaml.v2
[INFO]Writing configuration file (glide.yaml)
接下来就可以自己去配置glide.yaml文件了(下面是初始化后我的glide.yaml)
package:.
import:
-package:github.com/buaazp/fasthttprouter
-package:github.com/tinylib/msgp
subpackages:
-msgp
-package:github.com/valyala/fasthttp
-package:gopkg.in/redis.v5
-package:gopkg.in/yaml.v2
解析下载包依赖
glide update(up)
glide会把配置文件里的第三方包下载到当前目录的vendor文件夹下。
接下来就可以go build。然而,报错了。
eventMain.go:24:2: cannot find package “github.com/buaazp/fasthttprouter” in any of:
/usr/local/Cellar/go/1.8.3/libexec/src/github.com/buaazp/fasthttprouter (from $GOROOT)
/Users/shaozhenyu/go/src/github.com/buaazp/fasthttprouter (from $GOPATH)
/Users/shaozhenyu/test/event/src/github.com/buaazp/fasthttprouter
eventMain.go:25:2: cannot find package “github.com/valyala/fasthttp” in any of:
/usr/local/Cellar/go/1.8.3/libexec/src/github.com/valyala/fasthttp (from $GOROOT)
/Users/shaozhenyu/go/src/github.com/valyala/fasthttp (from $GOPATH)
/Users/shaozhenyu/test/event/src/github.com/valyala/fasthttp
google了一下,发现问题,是我的目录结构不对,根本的问题是:
vendor目录不能放到 工作空间源码包目录的根目录($GOPATH/src)之下,必须放到 某个(项目)文件夹之下。
所以需要在src再新建一个目录,然后把这个项目放到该目录下即可,想详细了解这个问题的同学可以看下面这个链接:
www.cnblogs.com/phpgo/p/6367738.html
go build 大功告成。
在编译过程中还遇到一个问题:
vendor/gopkg.in/redis.v5/command.go:10:2: use of internal package not allowed
vendor/gopkg.in/redis.v5/ring.go:13:2: use of internal package not allowed
vendor/gopkg.in/redis.v5/ring.go:14:2: use of internal package not allowed
vendor/gopkg.in/redis.v5/command.go:11:2: use of internal package not allowed
vendor/gopkg.in/redis.v5/command.go:12:2: use of internal package not allowed
被这个问题浪费了很多时间,查了很多资料也没有解决。
最后把~/.glide(存的是下载的第三方库的缓存)中的东西全部删掉,把项目中的glide.yaml, glide.lock, vendor全部删掉。
全部重来,然后就解决了。
glide的其他命令暂时还没用到,后续如果有用到会更新上去。
总结
glide确实是golang第三方包管理工具,方便版本控制,和团队使用。