Golang包管理工具(一)之govendor的使用

來源:互聯網
上載者:User

本文個人部落格地址為:http://www.huweihuang.com/article/golang/govendor-usage/
本文個人部落格地址為:https://huweihuang.net/article/golang/govendor-usage/

更多golang包管理工具參考
- dep的使用
- glide的使用
- govendor的使用

1. govendor簡介

golang工程的依賴包經常使用go get命令來擷取,例如:go get github.com/kardianos/govendor ,會將依賴包下載到GOPATH的路徑下。

常用的依賴包管理工具有godepgovendor等,在Golang1.5之後,Go提供了 GO15VENDOREXPERIMENT 環境變數(Go 1.6版本預設開啟該環境變數),用於將go build時的應用路徑搜尋調整成為 當前項目目錄/vendor 目錄方式。通過這種形式,我們可以實作類別似於 godep 方式的項目依賴管理。

2. 安裝與使用

2.1. 安裝

go get -u -v github.com/kardianos/govendor

2.2. 使用

#進入到項目目錄cd /home/gopath/src/mytool#初始化vendor目錄govendor init#查看vendor目錄[root@CC54425A mytool]# lscommands  main.go  vendor  mytool_test.sh#將GOPATH中本工程使用到的依賴包自動移動到vendor目錄中#說明:如果本地GOPATH沒有依賴包,先go get相應的依賴包govendor add +external或使用縮寫: govendor add +e #Go 1.6以上版本預設開啟 GO15VENDOREXPERIMENT 環境變數,可忽略該步驟。#通過設定環境變數 GO15VENDOREXPERIMENT=1 使用vendor檔案夾構建檔案。#可以選擇 export GO15VENDOREXPERIMENT=1 或 GO15VENDOREXPERIMENT=1 go build 執行編譯export GO15VENDOREXPERIMENT=1

2.3. 說明

govendor只是用來管理項目的依賴包,如果GOPATH中本身沒有項目的依賴包,則需要通過go get先下載到GOPATH中,再通過govendor add +external拷貝到vendor目錄中。Go 1.6以上版本預設開啟GO15VENDOREXPERIMENT環境變數

3. govendor使用命令

[root@CC54425A mytool]# govendorgovendor (v1.0.8): record dependencies and copy into vendor folder    -govendor-licenses    Show govendor's licenses.    -version              Show govendor version    -cpuprofile 'file'    Writes a CPU profile to 'file' for debugging.    -memprofile 'file'    Writes a heap profile to 'file' for debugging.Sub-Commands    init     Create the "vendor" folder and the "vendor.json" file.    list     List and filter existing dependencies and packages.    add      Add packages from $GOPATH.    update   Update packages from $GOPATH.    remove   Remove packages from the vendor folder.    status   Lists any packages missing, out-of-date, or modified locally.    fetch    Add new or update vendor folder packages from remote repository.    sync     Pull packages into vendor folder from remote repository with revisions                 from vendor.json file.    migrate  Move packages from a legacy tool to the vendor folder with metadata.    get      Like "go get" but copies dependencies into a "vendor" folder.    license  List discovered licenses for the given status or import paths.    shell    Run a "shell" to make multiple sub-commands more efficient for large                 projects.    go tool commands that are wrapped:      "+status" package selection may be used with them    fmt, build, install, clean, test, vet, generate, toolStatus Types    +local    (l) packages in your project    +external (e) referenced packages in GOPATH but not in current project    +vendor   (v) packages in the vendor folder    +std      (s) packages in the standard library    +excluded (x) external packages explicitly excluded from vendoring    +unused   (u) packages in the vendor folder, but unused    +missing  (m) referenced packages but not found    +program  (p) package is a main package    +outside  +external +missing    +all      +all packages    Status can be referenced by their initial letters.Package specifier    <path>[::<origin>][{/...|/^}][@[<version-spec>]]Ignoring files with build tags, or excluding packages from being vendored:    The "vendor.json" file contains a string field named "ignore".    It may contain a space separated list of build tags to ignore when    listing and copying files.    This list may also contain package prefixes (containing a "/", possibly    as last character) to exclude when copying files in the vendor folder.    If "foo/" appears in this field, then package "foo" and all its sub-packages    ("foo/bar", …) will be excluded (but package "bar/foo" will not).    By default the init command adds the "test" tag to the ignore list.If using go1.5, ensure GO15VENDOREXPERIMENT=1 is set.

4. vendor.json

govendor設定檔,記錄依賴包列表。

{    "comment": "",    "ignore": "test",    "package": [        {            "checksumSHA1": "uGalSICR4r7354vvKuNnh7Y/R/4=",            "path": "github.com/urfave/cli",            "revision": "b99aa811b4c1dd84cc6bccb8499c82c72098085a",            "revisionTime": "2017-08-04T09:34:15Z"        }    ],    "rootPath": "mytool"}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.