搜鞥都哦 发表于 2018-9-21 08:03:07

使用glide进行包管理

  glide是golang的一款包管理工具,就像Java下的Maven(当然Maven的定位不只是包管理工具)。今天试用了一下,记录一下使用过程和遇到的一些坑。

环境
  Windows 7,Goland,Go 1.8.4

安装
  

go get -u github.com/Masterminds/glide  

  在Goland中打开File——Settings——Tools——External Tools,根据自己的gopath添加gopath\bin\glide.exe,如图
  

  在Goland中输入命令:
  

glide  

  如果出现提示就证明安装成功了。

使用
  不多说,直接上命令:
  

glide init  

  然后就会生成对应的glide.yaml,生成过程中根据自己需要选择合适的配置就行。
  
我的glide.yaml如下:
  

package: github.com/AceDarkkinght/GoProxyCollector  
import:
  
- package: github.com/PuerkitoBio/goquery
  version: ~1.3.0
  
- package: github.com/boltdb/bolt
  version: ~1.3.1
  
- package: github.com/cihub/seelog
  version: ~2.6.0
  
- package: github.com/parnurzeal/gorequest
  version: ~0.2.15
  

  现在就可以通过命令下载对应的dependencies了
  

glide install  

  然后就毫无意外的出错了。。。。
  

Unable to set version on golang.org/x/net/html to . Err: Cannot detect VCS  
Error scanning golang.org\x\net\html: open C:\Users\yourUserName\.glide\cache\src\https-golang.org-x-net-html: The system cannot find the file specif
  
ied.
  
Failed to retrieve a list of dependencies: Error resolving imports
  

  

  这是由于墙的原因导致有些google的包下不下来,我们可以使用mirror命令指定镜像,从镜像地址下载。命令如下:
  

glide mirror set https://golang.org/x/net/html https://github.com/golang/net --vcs git  
glide mirror set https://golang.org/x/net/publicsuffix https://github.com/golang/net --vcs git
  
glide mirror set https://golang.org/x/sys/unix https://github.com/golang/sys --vcs git
  

  

  再次运行:
  

glide install  

  再出错:
  

Unable to export dependencies to vendor directory: Error moving files: exit status 1. output: Access is denied.  0 dir(s) moved.
  

  

  查了资料发现这是Windows下和文件权限有关的一个bug,github上面有相关讨论:
  
https://github.com/Masterminds/glide/issues/873
  
网友给出了两个解决方案:


[*]修改glide源代码。
[*]修改系统UAC。
  这里采用第一种方法:
  

// 找到github.com/Masterminds/glide/path/winbug.go  
// 修改 func CustomRename(o, n string) error
  
// 把 cmd := exec.Command("cmd.exe", "/c", "move", o, n) 改为
  
cmd := exec.Command("robocopy.exe", o, n, "/e")
  

  保存修改,重新编译。
  

go get -u github.com/Masterminds/glide  

  然后运行:
  

glide install  

  如无意外的话你就可以在项目路径看到vendor文件夹了。
  如果你有更好的办法或者遇到了别的问题也欢迎一起讨论。

Reference
  https://studygolang.com/articles/7129
  
https://github.com/Masterminds/glide/issues/873
  
https://my.oschina.net/quicker/blog/831352?hmsr=studygolang.com&utm_medium=studygolang.com&utm_source=studygolang.com


页: [1]
查看完整版本: 使用glide进行包管理