Golang依賴關(guān)聯(lián)工具:glide從入門到精通使用

介紹

不論是開發(fā)Java還是你正在學(xué)習(xí)的Golang,都會(huì)遇到依賴管理問題。Java有牛逼轟轟的Maven和Gradle。 Golang亦有g(shù)odep、govendor、glide、gvt、gopack等等,本文主要給大家介紹gilde。 glide是Golang的包管理工具,是為了解決Golang依賴問題的。

創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供鐵鋒網(wǎng)站建設(shè)、鐵鋒做網(wǎng)站、鐵鋒網(wǎng)站設(shè)計(jì)、鐵鋒網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、鐵鋒企業(yè)網(wǎng)站模板建站服務(wù),十年鐵鋒做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。

安裝

Golang環(huán)境設(shè)置

Golang 1.5版本后才采用vendor目錄特性。

//設(shè)置環(huán)境變量使用vendor目錄
GO15VENDOREXPERIMENT=1

安裝glide

$ go get github.com/Masterminds/glide
$ go install github.com/Masterminds/glide

驗(yàn)證

$ glide
NAME:
   glide -Vendor Package Management for your Go projects.

   Each projectshould have a 'glide.yaml' file in the project directory. Files
   looksomething 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 moredetails on the 'glide.yaml' files see the documentation at
  https://glide.sh/docs/glide.yaml


USAGE:
   glide [globaloptions] 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 thatmakes optional suggestions to improve config in a glide.yaml file.
     get                Install one or more packagesinto `vendor/` and add dependency to glide.yaml.
     remove,rm         Remove a package from theglide.yaml file, and regenerate the lock file.
     import             Import files from other dependencymanagement systems.
     name               Print the name of this project.
     novendor,nv       List all non-vendor paths in adirectory.
    rebuild            Rebuild ('gobuild') the dependencies
     install,i         Install a project'sdependencies
     update,up         Update a project'sdependencies
     tree               (Deprecated) Tree prints thedependencies of this project as a tree.
     list               List prints all dependenciesthat the present code references.
     info               Info prints information aboutthis project
    cache-clear, cc    Clears theGlide cache.
     about              Learn about Glide
     mirror             Manage mirrors
     help,h            Shows a list of commands orhelp for one command

GLOBAL OPTIONS:
   --yaml value,-y value  Set a YAML configuration file.(default: "glide.yaml")
   --quiet,-q             Quiet (no info or debugmessages)
   --debug                 Print debug verboseinformational messages
   --homevalue            The location of Glidefiles (default: "/home/users/qiangmzsx/.glide") [$GLIDE_HOME]
   --tmpvalue             The temp directory touse. Defaults to systems temp [$GLIDE_TMP]
  --no-color              Turn offcolored output for log messages
   --help,-h              show help
   --version,-v           print the version

看到這樣,那就恭喜你,已經(jīng)安裝成功了!??!

使用

篇幅有限,我只介紹經(jīng)常使用到的。先進(jìn)入在GOPATH的一個(gè)項(xiàng)目中。

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 toskip)
[INFO]  Scanningcode to look for dependencies
[INFO]  -->Found reference to github.com/urfave/cli
[INFO]  Writingconfiguration file (glide.yaml)
[INFO]  Wouldyou like Glide to help you find ways to improve your glide.yaml configuration?
[INFO]  If youwant to revisit this step you can use the config-wizard command at any time.
[INFO]  Yes (Y)or No (N)?
Y
[INFO]  Loadingmirrors from mirrors.yaml file
[INFO]  Lookingfor 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 aresome suggestions...
[INFO]  Thepackage github.com/urfave/cli appears to have Semantic Version releases(http://semver.org).
[INFO]  Thelatest release is v1.19.1. You are currently not using a release. Would youlike
[INFO]  to usethis release? Yes (Y) or No (N)
Y
[INFO]  Wouldyou like to remember the previous decision and apply it to future
[INFO] dependencies? Yes (Y) or No (N)
Y
[INFO]  Updatinggithub.com/urfave/cli to use the release v1.19.1 instead of no release
[INFO]  Thepackage github.com/urfave/cli appears to use semantic versions(http://semver.org).
[INFO]  Wouldyou like to track the latest minor or patch releases (major.minor.patch)?
[INFO]  Trackingminor version releases would use '>= 1.19.1, < 2.0.0' ('^1.19.1').Tracking patch version
[INFO]  releaseswould use '>= 1.19.1, < 1.20.0' ('~1.19.1'). For more information onGlide versions
[INFO]  andranges see https://glide.sh/docs/versions
[INFO]  Minor(M), Patch (P), or Skip Ranges (S)?
P
[INFO]  Wouldyou like to remember the previous decision and apply it to future
[INFO] dependencies? Yes (Y) or No (N)
Y
[INFO]  Updatinggithub.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]  changesto your configuration file? Yes (Y) or No (N)
Y
[INFO]  Writingupdates to configuration file (glide.yaml)
[INFO]  You cannow 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 會(huì)詢問一些問題。 glide.yaml記載了依賴包的列表及其更新規(guī)則,每次執(zhí)行 glide up 時(shí),都會(huì)按照指定的規(guī)則(如只下載補(bǔ)丁(patch)不下載升級(jí)(minor))下載新版。 

一個(gè)完整的gilde.yaml

package: foor
homepage:https://github.com/qiangmzsx
license: MIT
owners:
-name: qiangmzsx
  email: qiangmzsx@hotmail.com
  homepage:https://github.com/qiangmzsx
# 去除包
ignore:
- appengine
- golang.org/x/net
# 排除目錄
excludeDirs:
- node_modules
# 導(dǎo)入包
import:
-package:github.com/astaxie/beego
  version: 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/goconvey
  subpackages:
  - convey

很多人看著yaml很不習(xí)慣,沒事,我轉(zhuǎn)一下json給大家看看。

{
  "excludeDirs":[
    "node_modules"
  ],
  "owners":[
    {
      "homepage":"https://github.com/qiangmzsx",
      "name":"qiangmzsx",
      "email":"qiangmzsx@hotmail.com"
    }
  ],
  
  "license":"MIT",
  "package":"foor",
  "ignore":[
    "appengine",
    "golang.org/x/net"
  ],
  "import":[
    {
      "version":"1.8.0",
      "package":"github.com/astaxie/beego"
    },
    {
      "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":[
    {
      "subpackages":[
        "convey"
      ],
      "package":"github.com/smartystreets/goconvey"
    }
  ],
  "homepage":"https://github.com/qiangmzsx"
}

版本號(hào)指定規(guī)則

=: equal (aliasedto no operator)
!=: not equal
>: greater than
<: less than
>=: greater than or equal to
<=: less than or equal to

1.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

指定版本報(bào)錯(cuò),需要用’’指定的可以不填寫

安裝依賴 (glide install)

glide.yaml我們已經(jīng)準(zhǔn)備好了,現(xiàn)在就改安裝一下試試。

$ glide install
[ERROR] Failed to parse/home/users/xxxx/golang/src/foor/glide.yaml: yaml: invalid leading UTF-8 octet
報(bào)錯(cuò)了!別擔(dān)心看看你的yaml文件是否為utf-8編碼,不是就轉(zhuǎn)換一下就好啦!
$ glide install
[INFO]  Lockfile (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 tov1.8.0
[INFO] Resolving imports
[INFO] Downloading dependencies. Please wait...
[INFO]  Settingreferences 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]  --> Exportinggithub.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]  Projectrelies on 6 dependencies.
$ ll
total 12
glide.lock
glide.yaml
vendor
$ ll vendor/
git.oschina.net
github.com

看到glide.look了嗎,這個(gè)文件記載了依賴包確定的revision, 下次再執(zhí)行 glide install 時(shí),會(huì)直接讀這個(gè)文件下載確定的版本。

升級(jí)版本 (glide up)

glide up 會(huì)按照語義化版本規(guī)則更新依賴包代碼,開發(fā)過程中如果需要使用新版代碼,可以執(zhí)行這個(gè)命令:修改一下glide.yaml中的一個(gè)Package.

- package:github.com/astaxie/beego
  version: 1.8.3

執(zhí)行g(shù)lide 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 tov1.8.3
[INFO] Resolving imports
[INFO] Downloading dependencies. Please wait...
[INFO]  Settingreferences 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]  Projectrelies on 6 dependencies.

添加并下載依賴 (glide get)

除了自動(dòng)從代碼中解析 import 外,glide 還可以通過 glide get 直接下載代碼中沒有的依賴,與 go get 的用法基本一致:

$ glide getgithub.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 tov1.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: Cannotdetect VCS
[ERROR] Failed to do initial checkout of config:Cannot detect VCS

這幾行信息估計(jì)很多人都是遇到過的。在我天朝或者在公司內(nèi)部都可能不能訪問一些站點(diǎn),導(dǎo)致很Golang的依賴包不能通過go get下載。此時(shí)也就是glide大發(fā)神威的時(shí)候到了,可以通過配置將墻了的版本庫 URL 映射到?jīng)]被墻的 URL,甚至也可以映射到本地版本庫。將golang.org映射到github: 修改glide.yaml加入

- package:golang.org/x/crypto

如果你的網(wǎng)絡(luò)可以訪問就不需要使用glide鏡像功能,可以跳過。

$ glide mirrorset 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]  Loadingmirrors 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 tov1.8.3
[INFO] Resolving imports
[INFO] Downloading dependencies. Please wait...
[INFO]  Settingreferences 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]  Projectrelies on 8 dependencies.
$ ll vendor/
git.oschina.net
github.com
golang.org

終于看到golang.org啦?。?!細(xì)心的你一定已經(jīng)發(fā)現(xiàn)了

[INFO]  mirrors.yaml written with changes

說明執(zhí)行glide mirror時(shí)候鏡像配置寫入到的是$HOME/.glide/mirrors.yaml中,打開看看。

repos:
-original: golang.org/x/crypto
  repo: github.com/golang/crypto

還可以映射到本地目錄。推薦大家可以去https://www.golangtc.com/download/package下載很多Golang類庫?,F(xiàn)在我去下載了:https://www.golangtc.com/static/download/packages/golang.org.x.text.tar.gz,解壓到本地目錄/home/users/qiangmzsx/var/golang/golang.org/x/text。

$ glide mirrorset golang.org/x/text /home/users/qiangmzsx/var/golang/golang.org/x/text
[INFO]  golang.org/x/textbeing set to /home/users/qiangmzsx/var/golang/golang.org/x/text
[INFO] mirrors.yaml written with changes
$ glide up
[INFO]  Loadingmirrors 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 tov1.8.3
[INFO] Resolving imports
[INFO] Downloading dependencies. Please wait...
[INFO]  Settingreferences 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]  Projectrelies on 9 dependencies.

全局選項(xiàng)

運(yùn)行g(shù)lide,在最后就可以看到

GLOBAL OPTIONS:
   --yaml value,-y value  Set a YAML configuration file.(default: "glide.yaml")
   --quiet,-q             Quiet (no info or debugmessages)
   --debug                 Print debug verboseinformational messages
   --homevalue            The location of Glidefiles (default: "/home/users/qiangmzsx/.glide") [$GLIDE_HOME]
   --tmpvalue             The temp directory touse. Defaults to systems temp [$GLIDE_TMP]
  --no-color              Turn offcolored output for log messages
   --help,-h              show help
   --version,-v           print the version

如果大家想把glide的yaml文件換別的默認(rèn)名稱可以執(zhí)行

 $ glide -y qiangmzsx.yaml

在官網(wǎng)中會(huì)看到一個(gè)GLIDE_HOME變量,該變量就是/home/users/qiangmzsx/.glide。這個(gè)目錄之前有提到過,除了包含有mirrors.yaml還有一個(gè)很重要的目錄cache本地 cache,每次更新代碼時(shí), glide 都會(huì)在本地保存 cache,以備下次 glide install 使用。

GLIDE_HOME可以通過如下命令修改。

 $ glide --home /home/glide

總結(jié)

除了上述說到的功能,glide還有很多好的功能,后續(xù)有機(jī)會(huì)在寫出來吧。
總結(jié)一下,glide是一款功能豐富,完全滿足需求的依賴管理工具,強(qiáng)烈大家使用。

文章題目:Golang依賴關(guān)聯(lián)工具:glide從入門到精通使用
標(biāo)題鏈接:http://muchs.cn/article22/iiohcc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計(jì)、營銷型網(wǎng)站建設(shè)、App設(shè)計(jì)、全網(wǎng)營銷推廣、電子商務(wù)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司