golang go get 安装私有gitlab上的包
当golang mod管理包时是非常好用的,直接一个go get 就能安装包
需求:安装私有的gitlab上的包时,直接用go get就没办直接安装
但是官方也是提供了安装方法,这里贴上方法
一般情况下,需要设置go get 的代理源为国内源
go env -w GOPROXY="https://mirrors.aliyun.com/goproxy/"
设置env变量,让私有gitlab不走代理
go env -w GOPRIVATE="gitlab.test.com"
go env -w GONOPROXY="gitlab.test.com"
go env -w GONOSUMDB="gitlab.test.com"
此时安装这个域名下的包就不走代理了
方法一(win下 安装包+vendor)
私有云一般都是有鉴权的,此时推荐方案为 vendor模式
- 直接安装报错
[root] go get -v gitlab.test.com/test-core@v0.0.2
get "gitlab.test.com/test-core": found meta tag vcs.metaImport{Prefix:"gitlab.test.com/test-core", VCS:"git", RepoRoot:"https://gitlab.test.com/test-core.git"} at //gitlab.test.com/test-core?go-get=1
go: gitlab.test.com/test-core@v0.0.2: invalid version: git ls-remote -q origin in D:\goWork\pkg\mod\cache\vcs\198ec5b986b2b08f3f67090af8ecb5a5635be9300255f6cf62a529e128de4584: exit status 128:
fatal: Cannot prompt because user interactivity has been disabled.
fatal: could not read Username for 'https://gitlab.test.com': terminal prompts disabled
Confirm the import path was entered correctly.
If this is a private repository, see https://golang.org/doc/faq#git_https for additional information.
这是因为私有gitlab都有鉴权的,且go get 的是以http的方式clone git库的。故我们在win模式下进行密钥保存
- 随便clone一个私有gitlab的库(http)
为什么需要进行http协议的clone,这是因为go get 时,已经禁止了交互了,所以没办法弹窗给用户输入账目密码了

此时会弹出一个框让你输入私有gitlab的账号密码,输完保存即可完成项目的clone。
- 再次运行命令,会发现鉴权以自动完成
[root] go get -v gitlab.test.com/test-core@v0.0.2
get "gitlab.test.com/test-core": found meta tag vcs.metaImport{Prefix:"gitlab.test.com/test-core", VCS:"git", RepoRoot:"https://gitlab.test.com/test-core.git"} at //gitlab.test.com/test-core?go-get=1
go: downloading gitlab.test.com/test-core v0.0.2
- 运行生成vendor目录
vendor模式是,这样包就是跟着项目走了,无论是win还是Linux,编译都是不受依赖包的影响了
go mod vendor
此模式总结为:win环境 go get 私有gitlab的包,vendor模式可以跨平台编译
835

被折叠的 条评论
为什么被折叠?



