Glide--------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依赖初始化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
[lisea@lisea  test ]$ glide init
[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 /pkg/sftp
[INFO]  --> Found reference to golang.org /x/crypto/ssh
[INFO]  Writing configuration  file  (glide.yaml)
[INFO]  Would you like Glide to help you  find  ways to improve your glide.yaml configuration?
[INFO]  If you want to revisit this step you can use the config-wizard  command  at any  time .
[INFO]  Yes (Y) or No (N)?
Y
[INFO]  Loading mirrors from mirrors.yaml  file
[INFO]  Looking  for  dependencies to  make  suggestions on
[INFO]  --> Scanning  for  dependencies not using version ranges
[INFO]  --> Scanning  for  dependencies using commit ids
[INFO]  Gathering information on each dependency
[INFO]  --> This may take a moment. Especially on a codebase with many dependencies
[INFO]  --> Gathering release information  for  dependencies
[INFO]  --> Looking  for  dependency imports where versions are commit ids
Y
[INFO]  Here are some suggestions...
[INFO]  The package github.com /pkg/sftp  appears to have Semantic Version releases (http: //semver .org). 
[INFO]  The latest release is 1.2.0. You are currently not using a release. Would you like
[INFO]  to use this release? Yes (Y) or No (N)
[INFO]  Would you like to remember the previous decision and apply it to future
[INFO]  dependencies? Yes (Y) or No (N)
Y
[INFO]  Updating github.com /pkg/sftp  to use the release 1.2.0 instead of no release
[INFO]  The package github.com /pkg/sftp  appears to use semantic versions (http: //semver .org).
[INFO]  Would you like to track the latest minor or patch releases (major.minor.patch)?
[INFO]  Tracking minor version releases would use  '>= 1.2.0, < 2.0.0'  ( '^1.2.0' ). Tracking patch version
[INFO]  releases would use  '>= 1.2.0, < 1.3.0'  ( '~1.2.0' ). For  more  information on Glide versions
[INFO]  and ranges see https: //glide .sh /docs/versions
[INFO]  Minor (M), Patch (P), or Skip Ranges (S)?
P
[INFO]  Would you like to remember the previous decision and apply it to future
[INFO]  dependencies? Yes (Y) or No (N)
Y
[INFO]  Updating github.com /pkg/sftp  to use the range ~1.2.0 instead of commit  id  1.2.0
[INFO]  Configuration changes have been made. Would you like to write these
[INFO]  changes to your configuration  file ? Yes (Y) or No (N)
Y
[INFO]  Writing updates to configuration  file  (glide.yaml)
[INFO]  You can now edit the glide.yaml  file .:
[INFO]  --> For  more  information on versions and ranges see https: //glide .sh /docs/versions/
[INFO]  --> For details on additional metadata see https: //glide .sh /docs/glide .yaml/

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

1
2
3
4
5
6
[lisea@lisea  test ]$  cat  glide.yaml 
package:  test
import :
- package: github.com /pkg/sftp
   version: ~1.2.0
- package: golang.org /x/crypto/ssh

     * glide安装依赖[ERROR报错]

1
2
3
4
5
6
7
8
9
10
[lisea@lisea  test ]$ glide  install
[INFO]  Loading mirrors from mirrors.yaml  file
[INFO]  Lock  file  (glide.lock) does not exist. Performing update.
[INFO]  Loading mirrors from mirrors.yaml  file
[INFO]  Downloading dependencies. Please wait...
[INFO]  --> Fetching golang.org /x/crypto/ssh
[INFO]  --> Fetching updates  for  github.com /pkg/sftp
[WARN]  Unable to checkout golang.org /x/crypto/ssh
[ERROR] Update failed  for  golang.org /x/crypto/ssh : Cannot detect VCS
[ERROR] Failed to  do  initial checkout of config: Cannot detect VCS


3.  ERROR解决

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

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

1
2
3
4
5
package:  test
import :
- package: github.com /pkg/sftp
   version: ~1.2.0
- package: golang.org /x/crypto/ssh

修改为:

1
2
3
4
5
6
7
package:  test
import :
- package: github.com /pkg/sftp
   version: ~1.2.0
- package: golang.org /x/crypto
   subpackages:
   ssh

    * 重新更新下载依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[lisea@lisea  test ]$ glide up
[INFO]  Loading mirrors from mirrors.yaml  file
[INFO]  Downloading dependencies. Please wait...
[INFO]  --> Fetching updates  for  golang.org /x/crypto
[INFO]  --> Fetching updates  for  github.com /pkg/sftp
[INFO]  --> Detected semantic version. Setting version  for  github.com /pkg/sftp  to 1.2.0
[INFO]  Resolving imports
[INFO]  --> Fetching updates  for  github.com /kr/fs
[INFO]  --> Fetching updates  for  github.com /pkg/errors
[INFO]  Downloading dependencies. Please wait...
[INFO]  Setting references  for  remaining imports
[INFO]  Exporting resolved dependencies...
[INFO]  --> Exporting github.com /pkg/errors
[INFO]  --> Exporting github.com /pkg/sftp
[INFO]  --> Exporting golang.org /x/crypto
[INFO]  --> Exporting github.com /kr/fs
[INFO]  Replacing existing vendor dependencies
[INFO]  Project relies on 4 dependencies.

successfully 成功解决


4. 总结

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




      本文转自asd1123509133 51CTO博客,原文链接:http://blog.51cto.com/lisea/2065210,如需转载请自行联系原作者





相关文章
|
5月前
|
Java 编译器 Go
【Golang】(1)Go的运行流程步骤与包的概念
初次上手Go语言!先来了解它的运行流程吧! 在Go中对包的概念又有怎样不同的见解呢?
302 4
|
8月前
|
设计模式 Kubernetes Go
​​什么是Golang项目的“主包精简,逻辑外置”?​
“主包精简,逻辑外置”是Go语言项目的一种设计原则,强调将程序入口保持简单,核心逻辑拆分至其他包,以提升代码可维护性、可测试性及扩展性,适用于CLI工具、Web服务等场景。
182 7
|
Go
Golang的math包常用方法
这篇文章介绍了Golang的math包中的常量和常用方法,并通过示例代码展示了如何使用这些常量和方法。
399 87
Golang的math包常用方法
|
前端开发 中间件 Go
实践Golang语言N层应用架构
【10月更文挑战第2天】本文介绍了如何在Go语言中使用Gin框架实现N层体系结构,借鉴了J2EE平台的多层分布式应用程序模型。文章首先概述了N层体系结构的基本概念,接着详细列出了Go语言中对应的构件名称,包括前端框架(如Vue.js、React)、Gin的处理函数和中间件、依赖注入和配置管理、会话管理和ORM库(如gorm或ent)。最后,提供了具体的代码示例,展示了如何实现HTTP请求处理、会话管理和数据库操作。
289 1
|
Go
Golang语言之包依赖管理
这篇文章详细介绍了Go语言的包依赖管理工具,包括godep和go module的使用,以及如何在项目中使用go module进行依赖管理,还探讨了如何导入本地包和第三方库下载的软件包存放位置。
424 4
|
存储 Go
Golang语言基于go module方式管理包(package)
这篇文章详细介绍了Golang语言中基于go module方式管理包(package)的方法,包括Go Modules的发展历史、go module的介绍、常用命令和操作步骤,并通过代码示例展示了如何初始化项目、引入第三方包、组织代码结构以及运行测试。
639 3
|
Go
Golang语言基于GOPATH方式管理包(package)
这篇文章详细介绍了Golang语言中基于GOPATH方式管理包(package)的方法,包括包的概述、定义、引入格式、别名使用、匿名引入,以及如何快速入门自定义包,并通过具体代码案例展示了包的环境准备、代码编写、细节说明和程序运行。
237 3
|
NoSQL Java 测试技术
Golang内存分析工具gctrace和pprof实战
文章详细介绍了Golang的两个内存分析工具gctrace和pprof的使用方法,通过实例分析展示了如何通过gctrace跟踪GC的不同阶段耗时与内存量对比,以及如何使用pprof进行内存分析和调优。
615 0
Golang内存分析工具gctrace和pprof实战
|
机器学习/深度学习 存储 人工智能
Golang bytes 包学习
Golang bytes 包学习
174 3
|
Serverless Go C语言
函数计算产品使用问题之如何在Golang运行时环境中解决glibc依赖问题
函数计算产品作为一种事件驱动的全托管计算服务,让用户能够专注于业务逻辑的编写,而无需关心底层服务器的管理与运维。你可以有效地利用函数计算产品来支撑各类应用场景,从简单的数据处理到复杂的业务逻辑,实现快速、高效、低成本的云上部署与运维。以下是一些关于使用函数计算产品的合集和要点,帮助你更好地理解和应用这一服务。
380 1