Golang依赖管理工具:glide从入门到精通使用
介绍
不论是开发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)
安装
Golang环境设置
采用vendor目录特性,Go 1.5 做为试验特性加入(需要指定 GO15VENDOREXPERIMENT=1 环境变量),并在 Go 1.6 正式引入的一个概念。多数 go 依赖解决方案都基于它的。GO15VENDOREXPERIMENT 是 Go 1.5 版本新增的一个环境变量,如果将值改为 1 则表示启用。它可以将项目根目录名为 vendor 的目录添加到 Go 的库搜寻路径中,实现一个局部依赖的效果。
特性在 1.5 版本作为实验特性被添加,1.6 中默认被启用,1.7 移除变量加入标准中。
Go 提供了原始的 go get ,让第三方包管理可以基于 go get 做扩展。GO15VENDOREXPERIMENT 特性让局部依赖成为现实。Go 官方在给第三方包管理营造条件以及引导开发者用户至所推荐的方向,促进社区的繁荣。证明了一个语言技术的生态不仅仅只能靠官方或者取决于官方的完善程度。
//设置环境变量 使用vendor目录
GO15VENDOREXPERIMENT=1
为什么要选择glide? Glide 是众多实现 GO15VENDOREXPERIMENT 特性的包管理工具之一,但它是本文最为推荐的,具体为什么推荐它,原因很简单,因为它目前最受关注。 几大主要功能:
- 持久化依赖列表至配置文件中,包括依赖版本(支持范围限定)以及私人仓库等
- 持久化关系树至 lock 文件中(类似于 yarn 和 cargo),以重复拉取相同版本依赖
- 兼容 go get 所支持的版本控制系统:Git, Bzr, HG, and SVN
- 支持 GO15VENDOREXPERIMENT 特性,使得不同项目可以依赖相同项目的不同版本
- 可以导入其他工具配置,例如: Godep, GPM, Gom, and GB
安装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. Files
look something like this:
package: github.com/Masterminds/glide
imports:
- package: github.com/Masterminds/cookoo
version: 1.1.0
- package: github.com/kylelemons/go-gypsy
subpackages:
- yaml
For more details on the 'glide.yaml' files see the documentation at
https://glide.sh/docs/glide.yaml
USAGE:
glide [global options] command [command options] [arguments...]
VERSION:
0.13.0-dev
COMMANDS:
create, init Initialize a new project, creating a glide.yaml file
config-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 dependencies
install, i Install a project's dependencies
update, up Update a project's dependencies
tree (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 project
cache-clear, cc Clears the Glide cache.
about Learn about Glide
mirror Manage mirrors
help, h Shows a list of commands or help for one command
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
看到这样,那就恭喜你,已经安装成功了!!!
使用
篇幅有限,我只介绍经常使用到的。 先进入在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