Golang 依赖包管理工具---glide
安装glide
$ go get github.com/Masterminds/glide
$ go install github.com/Masterminds/glide
验证
$ glide
NAME:glide - Vendor Package Management for your Go projects.Each project should have a 'glide.yaml' file in the project directory. Fileslook something like this:package: github.com/Masterminds/glideimports:- package: github.com/Masterminds/cookooversion: 1.1.0- package: github.com/kylelemons/go-gypsysubpackages:- yamlFor more details on the 'glide.yaml' files see the documentation athttps://glide.sh/docs/glide.yamlUSAGE:glide [global options] command [command options] [arguments...]VERSION:0.13.0-devCOMMANDS:create, init Initialize a new project, creating a glide.yaml fileconfig-wizard, cw Wizard that makes optional suggestions to improve config in a glide.yaml file.get Install one or more packages into `vendor/` and add dependency to glide.yaml.remove, rm Remove a package from the glide.yaml file, and regenerate the lock file.import Import files from other dependency management systems.name Print the name of this project.novendor, nv List all non-vendor paths in a directory.rebuild Rebuild ('go build') the dependenciesinstall, i Install a project's dependenciesupdate, up Update a project's dependenciestree (Deprecated) Tree prints the dependencies of this project as a tree.list List prints all dependencies that the present code references.info Info prints information about this projectcache-clear, cc Clears the Glide cache.about Learn about Glidemirror Manage mirrorshelp, h Shows a list of commands or help for one commandGLOBAL OPTIONS:--yaml value, -y value Set a YAML configuration file. (default: "glide.yaml")--quiet, -q Quiet (no info or debug messages)--debug Print debug verbose informational messages--home value The location of Glide files (default: "/home/users/qiangmzsx/.glide") [$GLIDE_HOME]--tmp value The temp directory to use. Defaults to systems temp [$GLIDE_TMP]--no-color Turn off colored output for log messages--help, -h show help--version, -v print the version
看到这样,那就恭喜你,已经安装成功了!!!
使用
篇幅有限,我只介绍经常使用到的。 先进入在GOPATH的一个项目中。
cd $GOPATH/src/foor
初始化 (glide init)
$ 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/urfave/cli
[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
[INFO] Here are some suggestions...
[INFO] The package github.com/urfave/cli appears to have Semantic Version releases (http://semver.org).
[INFO] The latest release is v1.19.1. You are currently not using a release. Would you like
[INFO] to use this release? Yes (Y) or No (N)
Y
[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/urfave/cli to use the release v1.19.1 instead of no release
[INFO] The package github.com/urfave/cli 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.19.1, < 2.0.0' ('^1.19.1'). Tracking patch version
[INFO] releases would use '>= 1.19.1, < 1.20.0' ('~1.19.1'). 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/urfave/cli to use the range ~1.19.1 instead of commit id v1.19.1
[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/
$ ll
glide.yaml
$ cat glide.yaml
package: foor
import: []
在初始化过程中, glide 会询问一些问题。 glide.yaml记载了依赖包的列表及其更新规则,每次执行 glide up 时,都会按照指定的规则(如只下载补丁(patch)不下载升级(minor))下载新版。
一个完整的gilde.yaml
package: foor
homepage: https://github.com/qiangmzsx
license: MIT
owners:
- name: qiangmzsxemail: qiangmzsx@hotmail.comhomepage: https://github.com/qiangmzsx
# 去除包
ignore:
- appengine
- golang.org/x/net
# 排除目录
excludeDirs:
- node_modules
# 导入包
import:
- package: github.com/astaxie/beegoversion: 1.8.0
- package: github.com/coocood/freecache
- package: github.com/garyburd/redigo/redis
- package: github.com/go-sql-driver/mysql
- package: github.com/bitly/go-simplejson
- package: git.oschina.net/qiangmzsx/beegofreecache
testImport:
- package: github.com/smartystreets/goconveysubpackages:- convey
glide.yaml中的这些元素的解释如下:
package
:顶部的 package 是它所在GOPATH的位置,glide 将从该位置下开始导包。homepage
:该项目的详情页面。license
:许可证标识,可以是SPDX license字符串或文件路径。owners
:项目的所有者信息,便于接受漏洞信息。ignore
:忽略导入的包,注意是包而不是目录。excludeDirs
:排除扫描依赖的目录。import
:import 的包列表:package
:导入包的名称,必填。软件包名称遵循go工具所用的相同模式。这意味着:1、映射到VCS远程位置的软件包名称以.git,.bzr,.hg或.svn结尾。 例如,example.com/foo/pkg.git/subpkg。2、GitHub, BitBucket, Launchpad, IBM Bluemix Services, and Go on Google Source是特殊情况,不需要 VCS 扩展。version
:可以为semantic version, semantic version range, branch, tag 或者 commit id。repo
:如果包名称不是repo位置或这是一个私人存储库,它可以去这里。 该软件包将从repo签出并放在软件包名称指定的位置。 这允许使用fork。vcs
:要使用的VCS,如git,hg,bzr或svn。仅当无法从名称中检测到类型时才需要。例如,以.git或GitHub结尾的仓库可以被检测为Git。 对于Bitbucket的repo,我们可以联系API来发现类型。subpackages
:在存储库中使用的包的记录。这不包括存储库中的所有包,而是包括正在使用的包。os
:用于过滤的操作系统的列表。如果设置它将比较当前运行时操作系统与指定的操作系统,并且只有获取匹配的依赖。如果未设置过滤,则跳过。这些名称与构建标志和GOOS环境变量中使用的名称相同。arch
:用于过滤的体系结构列表。如果设置它将比较当前运行时架构与指定的架构,并且只有在匹配时获取依赖关系。如果未设置过滤,则跳过。名称与构建标志和GOARCH环境变量中使用的名称相同。
testImport
:在导入中未列出的测试中使用的软件包列表。每个包具有与导入下列出的相同的详细信息。
版本号指定规则
=: equal (aliased to no operator)
!=: not equal
>: greater than
<: less than
>=: greater than or equal to
<=: less than or equal to1.2 - 1.4.5 which is equivalent to >= 1.2, <= 1.4.5
2.3.4 - 4.5 which is equivalent to >= 2.3.4, <= 4.5
1.2.x is equivalent to >= 1.2.0, < 1.3.0>= 1.2.x is equivalent to >= 1.2.0
<= 2.x is equivalent to < 3
* is equivalent to >= 0.0.0~1.2.3 is equivalent to >= 1.2.3, < 1.3.0
~1 is equivalent to >= 1, < 2
~2.3 is equivalent to >= 2.3, < 2.4
~1.2.x is equivalent to >= 1.2.0, < 1.3.0
~1.x is equivalent to >= 1, < 2^1.2.3 is equivalent to >= 1.2.3, < 2.0.0
^1.2.x is equivalent to >= 1.2.0, < 2.0.0
^2.3 is equivalent to >= 2.3, < 3
^2.x is equivalent to >= 2.0.0, < 3
''指定版本报错,需要用''指定的可以不填写
安装依赖 (glide install)
glide.yaml我们已经准备好了,现在就改安装一下试试。
$ glide install
[ERROR] Failed to parse /home/users/xxxx/golang/src/foor/glide.yaml: yaml: invalid leading UTF-8 octet
报错了!别担心看看你的yaml文件是否为utf-8编码,不是就转换一下就好啦!
$ glide install
[INFO] Lock file (glide.lock) does not exist. Performing update.
[INFO] Downloading dependencies. Please wait...
[INFO] --> Fetching updates for github.com/go-sql-driver/mysql
[INFO] --> Fetching updates for github.com/astaxie/beego
[INFO] --> Fetching updates for github.com/coocood/freecache
[INFO] --> Fetching updates for git.oschina.net/qiangmzsx/beegofreecache
[INFO] --> Fetching updates for github.com/bitly/go-simplejson
[INFO] --> Fetching updates for github.com/garyburd/redigo
[INFO] --> Fetching updates for github.com/smartystreets/goconvey
[INFO] --> Detected semantic version. Setting version for github.com/astaxie/beego to v1.8.0
[INFO] Resolving imports
[INFO] Downloading dependencies. Please wait...
[INFO] Setting references for remaining imports
[INFO] Exporting resolved dependencies...
[INFO] --> Exporting github.com/astaxie/beego
[INFO] --> Exporting github.com/coocood/freecache
[INFO] --> Exporting github.com/bitly/go-simplejson
[INFO] --> Exporting github.com/go-sql-driver/mysql
[INFO] --> Exporting github.com/garyburd/redigo
[INFO] --> Exporting github.com/smartystreets/goconvey
[INFO] --> Exporting git.oschina.net/qiangmzsx/beegofreecache
[INFO] Replacing existing vendor dependencies
[INFO] Project relies on 6 dependencies.
$ ll
total 12
glide.lock
glide.yaml
vendor
$ ll vendor/
git.oschina.net
github.com
看到glide.look了吗,这个文件记载了依赖包确定的revision, 下次再执行 glide install 时,会直接读这个文件下载确定的版本。
升级版本 (glide up)
glide up 会按照语义化版本规则更新依赖包代码,开发过程中如果需要使用新版代码,可以执行这个命令: 修改一下glide.yaml中的一个Package.
- package: github.com/astaxie/beegoversion: 1.8.3
执行glide up。
$ glide up
[INFO] Downloading dependencies. Please wait...
[INFO] --> Fetching updates for git.oschina.net/qiangmzsx/beegofreecache
[INFO] --> Fetching updates for github.com/garyburd/redigo
[INFO] --> Fetching updates for github.com/go-sql-driver/mysql
[INFO] --> Fetching updates for github.com/astaxie/beego
[INFO] --> Fetching updates for github.com/bitly/go-simplejson
[INFO] --> Fetching updates for github.com/coocood/freecache
[INFO] --> Fetching updates for github.com/smartystreets/goconvey
[INFO] --> Detected semantic version. Setting version for github.com/astaxie/beego to v1.8.3
[INFO] Resolving imports
[INFO] Downloading dependencies. Please wait...
[INFO] Setting references for remaining imports
[INFO] Exporting resolved dependencies...
[INFO] --> Exporting github.com/astaxie/beego
[INFO] --> Exporting github.com/bitly/go-simplejson
[INFO] --> Exporting github.com/garyburd/redigo
[INFO] --> Exporting github.com/go-sql-driver/mysql
[INFO] --> Exporting github.com/coocood/freecache
[INFO] --> Exporting github.com/smartystreets/goconvey
[INFO] --> Exporting git.oschina.net/qiangmzsx/beegofreecache
[INFO] Replacing existing vendor dependencies
[INFO] Project relies on 6 dependencies.
添加并下载依赖 (glide get)
除了自动从代码中解析 import 外,glide 还可以通过 glide get 直接下载代码中没有的依赖,与 go get 的用法基本一致:
$ glide get github.com/orcaman/concurrent-map
[INFO] Preparing to install 1 package.
[INFO] Attempting to get package github.com/orcaman/concurrent-map
[INFO] --> Gathering release information for github.com/orcaman/concurrent-map
[INFO] --> Adding github.com/orcaman/concurrent-map to your configuration
[INFO] Downloading dependencies. Please wait...
[INFO] --> Fetching updates for github.com/garyburd/redigo
[INFO] --> Fetching updates for github.com/astaxie/beego
[INFO] --> Fetching updates for github.com/go-sql-driver/mysql
[INFO] --> Fetching updates for git.oschina.net/qiangmzsx/beegofreecache
[INFO] --> Fetching updates for github.com/bitly/go-simplejson
[INFO] --> Fetching github.com/orcaman/concurrent-map
[INFO] --> Fetching updates for github.com/coocood/freecache
[INFO] --> Fetching updates for github.com/smartystreets/goconvey
[INFO] Resolving imports
[INFO] Downloading dependencies. Please wait...
[INFO] --> Detected semantic version. Setting version for github.com/astaxie/beego to v1.8.3
[INFO] Exporting resolved dependencies...
[INFO] --> Exporting github.com/smartystreets/goconvey
[INFO] --> Exporting github.com/garyburd/redigo
[INFO] --> Exporting github.com/go-sql-driver/mysql
[INFO] --> Exporting github.com/orcaman/concurrent-map
[INFO] --> Exporting github.com/astaxie/beego
[INFO] --> Exporting github.com/bitly/go-simplejson
[INFO] --> Exporting github.com/coocood/freecache
[INFO] --> Exporting git.oschina.net/qiangmzsx/beegofreecache
[INFO] Replacing existing vendor dependencies
使用镜像 (glide mirror)
[WARN] Unable to checkout golang.org/x/crypto
[ERROR] Update failed for golang.org/x/crypto: Cannot detect VCS
[ERROR] Failed to do initial checkout of config: Cannot detect VCS
这几行信息估计很多人都是遇到过的。在我天朝或者在公司内部都可能不能访问一些站点,导致很Golang的依赖包不能通过go get下载。此时也就是glide大发神威的时候到了,可以通过配置将墙了的版本库 URL 映射到没被墙的 URL,甚至也可以映射到本地版本库。 将golang.org映射到github: 修改glide.yaml加入
- package: golang.org/x/crypto
如果你的网络可以访问就不需要使用glide镜像功能,可以跳过。
$ glide mirror set golang.org/x/crypto github.com/golang/crypto
[INFO] golang.org/x/crypto being set to github.com/golang/crypto
[INFO] mirrors.yaml written with changes
$ glide up
[INFO] Loading mirrors from mirrors.yaml file
[INFO] Downloading dependencies. Please wait...
[INFO] --> Fetching updates for github.com/orcaman/concurrent-map
[INFO] --> Fetching golang.org/x/crypto
[INFO] --> Fetching updates for github.com/astaxie/beego
[INFO] --> Fetching updates for github.com/go-sql-driver/mysql
[INFO] --> Fetching updates for github.com/garyburd/redigo
[INFO] --> Fetching updates for github.com/coocood/freecache
[INFO] --> Fetching updates for github.com/bitly/go-simplejson
[INFO] --> Fetching updates for git.oschina.net/qiangmzsx/beegofreecache
[INFO] --> Fetching updates for github.com/smartystreets/goconvey
[INFO] --> Detected semantic version. Setting version for github.com/astaxie/beego to v1.8.3
[INFO] Resolving imports
[INFO] Downloading dependencies. Please wait...
[INFO] Setting references for remaining imports
[INFO] Exporting resolved dependencies...
[INFO] --> Exporting github.com/astaxie/beego
[INFO] --> Exporting github.com/coocood/freecache
[INFO] --> Exporting github.com/smartystreets/goconvey
[INFO] --> Exporting github.com/garyburd/redigo
[INFO] --> Exporting github.com/go-sql-driver/mysql
[INFO] --> Exporting github.com/bitly/go-simplejson
[INFO] --> Exporting github.com/orcaman/concurrent-map
[INFO] --> Exporting golang.org/x/crypto
[INFO] --> Exporting git.oschina.net/qiangmzsx/beegofreecache
[INFO] Replacing existing vendor dependencies
[INFO] Project relies on 8 dependencies.
$ ll vendor/
git.oschina.net
github.com
golang.org
终于看到golang.org啦!!! 细心的你一定已经发现了
[INFO] mirrors.yaml written with changes
说明执行glide mirror时候镜像配置写入到的是$HOME/.glide/mirrors.yaml
中,打开看看。
repos:
- original: golang.org/x/cryptorepo: github.com/golang/crypto
还可以映射到本地目录。 推荐大家可以去https://www.golangtc.com/download/package下载很多Golang类库。 现在我去下载了:https://www.golangtc.com/static/download/packages/golang.org.x.text.tar.gz,解压到本地目录/home/users/qiangmzsx/var/golang/golang.org/x/text
。
$ glide mirror set golang.org/x/text /home/users/qiangmzsx/var/golang/golang.org/x/text
[INFO] golang.org/x/text being set to /home/users/qiangmzsx/var/golang/golang.org/x/text
[INFO] mirrors.yaml written with changes
$ glide up
[INFO] Loading mirrors from mirrors.yaml file
[INFO] Downloading dependencies. Please wait...
[INFO] --> Fetching golang.org/x/text
[INFO] --> Fetching updates for github.com/garyburd/redigo
[INFO] --> Fetching updates for git.oschina.net/qiangmzsx/beegofreecache
[INFO] --> Fetching updates for github.com/astaxie/beego
[INFO] --> Fetching updates for github.com/bitly/go-simplejson
[INFO] --> Fetching updates for github.com/go-sql-driver/mysql
[INFO] --> Fetching updates for github.com/coocood/freecache
[INFO] --> Fetching updates for github.com/orcaman/concurrent-map
[INFO] --> Fetching updates for golang.org/x/crypto
[INFO] --> Fetching updates for github.com/smartystreets/goconvey
[INFO] --> Detected semantic version. Setting version for github.com/astaxie/beego to v1.8.3
[INFO] Resolving imports
[INFO] Downloading dependencies. Please wait...
[INFO] Setting references for remaining imports
[INFO] Exporting resolved dependencies...
[INFO] --> Exporting github.com/astaxie/beego
[INFO] --> Exporting github.com/go-sql-driver/mysql
[INFO] --> Exporting github.com/bitly/go-simplejson
[INFO] --> Exporting github.com/coocood/freecache
[INFO] --> Exporting github.com/smartystreets/goconvey
[INFO] --> Exporting github.com/garyburd/redigo
[INFO] --> Exporting github.com/orcaman/concurrent-map
[INFO] --> Exporting golang.org/x/text
[INFO] --> Exporting golang.org/x/crypto
[INFO] --> Exporting git.oschina.net/qiangmzsx/beegofreecache
[INFO] Replacing existing vendor dependencies
[INFO] Project relies on 9 dependencies.
全局选项
运行glide,在最后就可以看到
GLOBAL OPTIONS:--yaml value, -y value Set a YAML configuration file. (default: "glide.yaml")--quiet, -q Quiet (no info or debug messages)--debug Print debug verbose informational messages--home value The location of Glide files (default: "/home/users/qiangmzsx/.glide") [$GLIDE_HOME]--tmp value The temp directory to use. Defaults to systems temp [$GLIDE_TMP]--no-color Turn off colored output for log messages--help, -h show help--version, -v print the version
如果大家想把glide的yaml文件换别的默认名称可以执行
$ glide -y qiangmzsx.yaml
在官网中会看到一个GLIDE_HOME变量,该变量就是/home/users/qiangmzsx/.glide
。 这个目录之前有提到过,除了包含有mirrors.yaml还有一个很重要的目录cache
本地 cache,每次更新代码时, glide 都会在本地保存 cache,以备下次 glide install 使用 。
GLIDE_HOME
可以通过如下命令修改。
$ glide --home /home/glide
总结
除了上述说到的功能,glide还有很多好的功能,后续有机会在写出来吧。
总结一下,glide是一款功能丰富,完全满足需求的依赖管理工具,强烈大家使用。
Golang 依赖包管理工具---glide相关推荐
- Go (Golang) 工具之依赖包管理工具goimports | Go语言规范-import规范(导入)排序 |GoLand:设置gofmt与goimports,保存时自动格式化代码
文章目录 Go (Golang) 工具之依赖包管理工具goimports Go语言规范-import(导入)排序 什么是goimports 安装和使用 Golang 使用goimports unrec ...
- Golang学习笔记之依赖包管理工具gvt
一.gvt概念 gvt全称为Go vendoring tool,可译为Go供应工具,或者意译为Go依赖包依赖包工具,whatever~~,简单来说就是可以方便的获取.更新.删除项目所依赖的工具包,这个 ...
- Cocoa依赖包管理工具
Carthage iOS 作为目前最大的移动端开发平台,开发者的生态环境也是非常繁荣.在 iOS 早期时候,开发者想要使用第三方库就必须将其源代码引入工程,或者说是以 git submodule 的形 ...
- Golang的包管理工具govendor 安装与使用
之前的项目使用的比较古老的dep(不是godep),能够根据项目引用的第三方包的情况自动download对应的库.当然国内对于有些包的下载需要翻墙,这个dep自动下载就不靠谱了,还得配置,无奈从其他地 ...
- golang中的包管理工具——govendor和godep简单学习
为什么用vendor目录 依赖问题 我们知道,一个工程稍大一点,通常会依赖各种各样的包.而Go使用统一的GOPATH管理依赖包,且每个包仅保留一个版本.而不同的依赖包由各自的版本工具独立管理,所以当所 ...
- 正在或即将被使用的Go依赖包管理方法:Go Modules,Go 1.13的标准特性
公众号原文地址:https://mp.weixin.qq.com/s/SGGV3tWEg5AAJ7I_FcK0cg 目录 目录 说明 初始化 依赖包的默认导入 依赖包的特定版本导入 查看已添加依赖 依 ...
- Go Modules依赖包管理与Go Modules实战
目录 Go Modules依赖包管理 Go Modules 简介 Go 包管理的历史 Go1.5 版本前:GOPATH Go1.5 版本:Vendoring "百花齐放":多种 G ...
- Go 语言编程 — go mod 依赖包管理
目录 文章目录 目录 go mod 依赖包管理 使用 go mod go mod 的子指令 go mod 依赖包管理 Golang 将 Modules(模块)定义为 "源代码交付和版本控制的 ...
- Golang包管理工具之govendor的使用
本文个人博客地址为:http://www.huweihuang.com/article/golang/govendor-usage/ 本文个人博客地址为:https://huweihuang.net/ ...
最新文章
- Java -- JDBC 学习--批量处理
- 在DB中存储图像-是或否?
- 移动端整屏滑动的实现
- Modern CSV中文版
- 什么是事务?为什么需要事务?
- 洛谷 P2197 nim游戏
- Python 一路走来 Django
- 黄聪:CodeSmith和PowerDesigner的使用安装和数据库创建(原创系列教程)
- java EE 监听器
- 05-01 docker 介绍
- yolov3 python接口_darknetyolov3调用Python接口来检测和保存批处理图像,DarknetYoloV3,python,进行,批量,图片,并...
- python标准数据类型复习
- 从神话诗歌到奇幻科学的人类探索史·《月亮》·总篇(简版)
- VFIO代码分析(5)VFIO-IOMMU驱动
- 低分辨率的显示器模拟高分辨率显示模式
- java生成xlsx文件
- Emlog模板fee2.0主题商业版
- Unity Loading转场学习笔记
- 2023 DiscuzX 酷二次元博客主题模板
- 基于Springboot实现的自动化测试平台