加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Glide--------Golang依赖包解决工具之错误实践

发布时间:2020-12-16 18:03:52 所属栏目:大数据 来源:网络整理
导读:1. 背景 不论是开发Java还是你正在学习的Golang,都会遇到 依赖管理 问题。Java有牛逼轰轰的Maven和Gradle。 Golang亦有godep、govendor、glide、gvt、gopack等等,本文主要给大家介绍 gilde 。 glide是Golang的包管理工具,是为了解决Golang依赖问题的。 为

1. 背景

不论是开发Java还是你正在学习的Golang,都会遇到依赖管理问题。Java有牛逼轰轰的Maven和Gradle。 Golang亦有godep、govendor、glide、gvt、gopack等等,本文主要给大家介绍gilde。 glide是Golang的包管理工具,是为了解决Golang依赖问题的。 为什么需要glide? 原因很简单,Go 语言原生包管理的缺陷。罗列一下golang的 get 子命令管理依赖有很多大缺陷:

* 能拉取源码的平台很有限,绝大多数依赖的是 github.com

* 不能区分版本,以至于令开发者以最后一项包名作为版本划分

* 依赖 列表/关系 无法持久化到本地,需要找出所有依赖包然后一个个 go get

* 只能依赖本地全局仓库(GOPATH/GOROOT),无法将库放置于局部仓库($PROJECT_HOME/vendor)


2. Error问题

项目中使用到了golang.org/x/crypto/ssh包,而由于国内网络原因,无法直接下载,需要提前从github.com/golang/crypto下载然后放到指定位置

* 项目中glide依赖初始化

[lisea@liseatest]$glideinit
[INFO]	GeneratingaYAMLconfigurationfileandguessingthedependencies
[INFO]	Attemptingtoimportfromotherpackagemanagers(use--skip-importtoskip)
[INFO]	Scanningcodetolookfordependencies
[INFO]	-->Foundreferencetogithub.com/pkg/sftp
[INFO]	-->Foundreferencetogolang.org/x/crypto/ssh
[INFO]	Writingconfigurationfile(glide.yaml)
[INFO]	WouldyoulikeGlidetohelpyoufindwaystoimproveyourglide.yamlconfiguration?
[INFO]	Ifyouwanttorevisitthisstepyoucanusetheconfig-wizardcommandatanytime.
[INFO]	Yes(Y)orNo(N)?
Y
[INFO]	Loadingmirrorsfrommirrors.yamlfile
[INFO]	Lookingfordependenciestomakesuggestionson
[INFO]	-->Scanningfordependenciesnotusingversionranges
[INFO]	-->Scanningfordependenciesusingcommitids
[INFO]	Gatheringinformationoneachdependency
[INFO]	-->Thismaytakeamoment.Especiallyonacodebasewithmanydependencies
[INFO]	-->Gatheringreleaseinformationfordependencies
[INFO]	-->Lookingfordependencyimportswhereversionsarecommitids
Y
[INFO]	Herearesomesuggestions...
[INFO]	Thepackagegithub.com/pkg/sftpappearstohaveSemanticVersionreleases(http://semver.org).
[INFO]	Thelatestreleaseis1.2.0.Youarecurrentlynotusingarelease.Wouldyoulike
[INFO]	tousethisrelease?Yes(Y)orNo(N)
[INFO]	Wouldyouliketorememberthepreviousdecisionandapplyittofuture
[INFO]	dependencies?Yes(Y)orNo(N)
Y
[INFO]	Updatinggithub.com/pkg/sftptousetherelease1.2.0insteadofnorelease
[INFO]	Thepackagegithub.com/pkg/sftpappearstousesemanticversions(http://semver.org).
[INFO]	Wouldyouliketotrackthelatestminororpatchreleases(major.minor.patch)?
[INFO]	Trackingminorversionreleaseswoulduse'>=1.2.0,<2.0.0'('^1.2.0').Trackingpatchversion
[INFO]	releaseswoulduse'>=1.2.0,<1.3.0'('~1.2.0').FormoreinformationonGlideversions
[INFO]	andrangesseehttps://glide.sh/docs/versions
[INFO]	Minor(M),Patch(P),orSkipRanges(S)?
P
[INFO]	Wouldyouliketorememberthepreviousdecisionandapplyittofuture
[INFO]	dependencies?Yes(Y)orNo(N)
Y
[INFO]	Updatinggithub.com/pkg/sftptousetherange~1.2.0insteadofcommitid1.2.0
[INFO]	Configurationchangeshavebeenmade.Wouldyouliketowritethese
[INFO]	changestoyourconfigurationfile?Yes(Y)orNo(N)
Y
[INFO]	Writingupdatestoconfigurationfile(glide.yaml)
[INFO]	Youcannowedittheglide.yamlfile.:
[INFO]	-->Formoreinformationonversionsandrangesseehttps://glide.sh/docs/versions/
[INFO]	-->Fordetailsonadditionalmetadataseehttps://glide.sh/docs/glide.yaml/

* 初始化后生成的依赖如下:(glide.yaml)

[lisea@liseatest]$catglide.yaml
package:test
import:
-package:github.com/pkg/sftp
version:~1.2.0
-package:golang.org/x/crypto/ssh

* glide安装依赖[ERROR报错]

[lisea@liseatest]$glideinstall
[INFO]	Loadingmirrorsfrommirrors.yamlfile
[INFO]	Lockfile(glide.lock)doesnotexist.Performingupdate.
[INFO]	Loadingmirrorsfrommirrors.yamlfile
[INFO]	Downloadingdependencies.Pleasewait...
[INFO]	-->Fetchinggolang.org/x/crypto/ssh
[INFO]	-->Fetchingupdatesforgithub.com/pkg/sftp
[WARN]	Unabletocheckoutgolang.org/x/crypto/ssh
[ERROR]	Updatefailedforgolang.org/x/crypto/ssh:CannotdetectVCS
[ERROR]	Failedtodoinitialcheckoutofconfig:CannotdetectVCS


3. ERROR解决

经通过度娘查询一圈发现,十个结果九个一样内容(在此鄙视抓内容的站点和纯copy的博主三秒种),最后在glide开源点github上的issue上找到解决方式

* 修改glide生成的glide.yaml文件

package:test
import:
-package:github.com/pkg/sftp
version:~1.2.0
-package:golang.org/x/crypto/ssh

修改为:

package:test
import:
-package:github.com/pkg/sftp
version:~1.2.0
-package:golang.org/x/crypto
subpackages:
-ssh

* 重新更新下载依赖

[lisea@liseatest]$glideup
[INFO]	Loadingmirrorsfrommirrors.yamlfile
[INFO]	Downloadingdependencies.Pleasewait...
[INFO]	-->Fetchingupdatesforgolang.org/x/crypto
[INFO]	-->Fetchingupdatesforgithub.com/pkg/sftp
[INFO]	-->Detectedsemanticversion.Settingversionforgithub.com/pkg/sftpto1.2.0
[INFO]	Resolvingimports
[INFO]	-->Fetchingupdatesforgithub.com/kr/fs
[INFO]	-->Fetchingupdatesforgithub.com/pkg/errors
[INFO]	Downloadingdependencies.Pleasewait...
[INFO]	Settingreferencesforremainingimports
[INFO]	Exportingresolveddependencies...
[INFO]	-->Exportinggithub.com/pkg/errors
[INFO]	-->Exportinggithub.com/pkg/sftp
[INFO]	-->Exportinggolang.org/x/crypto
[INFO]	-->Exportinggithub.com/kr/fs
[INFO]	Replacingexistingvendordependencies
[INFO]	Projectrelieson4dependencies.

successfully 成功解决


4. 总结

以需求驱动技术,技术本身没有优略之分,只有业务之分。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读