公司打算用glide管理第三方包屁桑,研究了一下他的使用方法,遇到一些坑,故記錄一下畔师。(環(huán)境 Darwin)
安裝
go get github.com/Masterminds/glide
初始化
glide init(create)
問(wèn)題:
工程目錄:?
src$echo $GOPATH
/Users/shaozhenyu/go
src$pwd
/Users/shaozhenyu/test/event/src
此時(shí)初始化的時(shí)候會(huì)把內(nèi)部的包都放進(jìn)去,顯然這是不對(duì)的
[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的時(shí)候需要把當(dāng)前目錄加入到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)
接下來(lái)就可以自己去配置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
解析下載包依賴(lài)
glide update(up)
glide會(huì)把配置文件里的第三方包下載到當(dāng)前目錄的vendor文件夾下牧牢。
接下來(lái)就可以go build看锉。然而,報(bào)錯(cuò)了塔鳍。
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了一下伯铣,發(fā)現(xiàn)問(wèn)題,是我的目錄結(jié)構(gòu)不對(duì)轮纫,根本的問(wèn)題是:
vendor目錄不能放到 工作空間源碼包目錄的根目錄($GOPATH/src)之下腔寡,必須放到 某個(gè)(項(xiàng)目)文件夾之下。
所以需要在src再新建一個(gè)目錄掌唾,然后把這個(gè)項(xiàng)目放到該目錄下即可放前,想詳細(xì)了解這個(gè)問(wèn)題的同學(xué)可以看下面這個(gè)鏈接:
www.cnblogs.com/phpgo/p/6367738.html
go build 大功告成。
在編譯過(guò)程中還遇到一個(gè)問(wèn)題:
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
被這個(gè)問(wèn)題浪費(fèi)了很多時(shí)間糯彬,查了很多資料也沒(méi)有解決凭语。
最后把~/.glide(存的是下載的第三方庫(kù)的緩存)中的東西全部刪掉,把項(xiàng)目中的glide.yaml, glide.lock, vendor全部刪掉撩扒。
全部重來(lái)似扔,然后就解決了。
glide的其他命令暫時(shí)還沒(méi)用到搓谆,后續(xù)如果有用到會(huì)更新上去炒辉。
總結(jié)
glide確實(shí)是golang第三方包管理工具,方便版本控制泉手,和團(tuán)隊(duì)使用黔寇。