介紹
不論是開發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 of