[TOC]
Dep
vendor
特性只用來存儲本地依賴
dep
為官方支持的實(shí)驗(yàn)性的工具宾袜,其官網(wǎng)地址.目前start已經(jīng)9673
個(gè)
其通過兩個(gè)metadata
文件來管理依賴:
Gopkg.toml
:定義用戶相關(guān)內(nèi)容吭练,如依賴的source
、branch
墅垮、version
等.可以通過命令生產(chǎn),也可以被用戶根據(jù) 需要手動修改Gopkg.lock
: 定義具體狀態(tài)逞泄,例如各依賴的revision
铛漓。自動生成的担租,不可以修改
安裝
直接從官網(wǎng)下載相應(yīng)平臺的二進(jìn)制包砸民,放入到環(huán)境變量中即可(為了使用更方便,可以進(jìn)行重命名一下)
使用
使用流程:
1. 創(chuàng)建項(xiàng)目
2. 執(zhí)行dep init
進(jìn)行初始化
3. 寫代碼并添加引用
其基本語法如下:
PS C:\Users\liukun> dep
Dep is a tool for managing dependencies for Go projects
Usage: "dep [command]"
Commands:
init Set up a new Go project, or migrate an existing one
status Report the status of the project's dependencies
ensure Ensure a dependency is safely vendored in the project
prune Pruning is now performed automatically by dep ensure.
version Show the dep version information
Examples:
dep init set up a new project
dep ensure install the project's dependencies
dep ensure -update update the locked versions of all dependencies
dep ensure -add github.com/pkg/errors add a dependency to the project
Use "dep help [command]" for more information about a command.
初始化使用:dep init
命令奋救,其會自動下載依賴包岭参,其會自動做以下事情
分析當(dāng)前代碼包中的依賴關(guān)系
將分析出的直接依賴/約束寫到
Gopkg.toml
將項(xiàng)目依賴的第三方包,在滿足Gopkg.toml中約束范圍內(nèi)的最新version/branch/revision信息寫入
Gopkg.lock
文件中創(chuàng)建
vendor
目錄尝艘,并以Gopkg.lock為輸入演侯,將其中包下載到vendor
下面
如果需手動添加一個(gè)具體的依賴,可以使用dep ensure -add xxx
建議直接使用dep ensure
背亥,其會自動在vendor
目錄中增加或更新相關(guān)依賴(add/update/remove)
查看狀態(tài):dep status
其緩存是放在$GOPATH/pkg/dep/sources里面
Dep通過兩個(gè)metadata文件來管理依賴: manifest文件Gopkg.toml
和lock文件Gopkg.lock
在$GOPATH/src
下創(chuàng)建項(xiàng)目目錄秒际,如foo
,執(zhí)行dep init
后狡汉,其下面會出現(xiàn)以下文件或者目錄
vendor
:目錄Gopkg.lock
:描述依賴的具體狀態(tài)娄徊,例如各依賴的revisionGopkg.toml
:描述用戶的意圖,包括依賴的 source盾戴、branch寄锐、version等
其關(guān)系如下:
文件語法
Gopkg.toml語法
Dependency rules
:constraints
和overrides
用于指定依賴的版本以及檢索地址Package graph rules
:required
和ignored
用于包含或排除其相應(yīng)的包metadata
:用戶定義的鍵值對map,其會被dep忽略prune
: 用于指定哪些文件或目錄不是必需的尖啡,其不會被放入到vendor
下
1.注釋: 使用#
2.required
:是一個(gè)包(不是projects)的列表橄仆,該包具有以下特性;
被項(xiàng)目用到
沒有被直接或者傳遞import
不想被加入到GOPAT中衅斩,也不想lock它的版本
其格式如下:
required = ["github.com/user/thing/cmd/thing"]
3.ignored
:是一個(gè)包(不是projects)的列表盆顾,避免某些package加入到依賴中,格式如下:
ignored = ["github.com/user/project/badpkg"]
4.constraint
: 指定直接依賴的相關(guān)信息畏梆。其格式如下
[[constraint]]
# Required: the root import path of the project being constrained.
name = "github.com/user/project"
# Recommended: the version constraint to enforce for the project.
# Note that only one of "branch", "version" or "revision" can be specified.
version = "1.0.0"
branch = "master"
revision = "abc123"
# Optional: an alternate location (URL or import path) for the project's source.
source = "https://github.com/myfork/package.git"
# Optional: metadata about the constraint or override that could be used by other independent systems
[metadata]
key1 = "value that convey data to other systems"
system1-data = "value that is used by a system"
system2-data = "value that is used by another system"
name
: 指定依賴的項(xiàng)目地址-
version|branch|revision
: 指定依賴的版本椎扬,這三者只能同時(shí)存在一個(gè)惫搏。-
version
:其對應(yīng)為tag
。是constraint
和version
中的屬性蚕涤,用來指定依賴的版本。其支持版本比較操作=
: equal!=
: not equal>
: greater than<
: less than>=
: greater than or equal to<=
: less than or equal to-
: literal range. E.g., 1.2 - 1.4.5 is equivalent to >= 1.2, <= 1.4.5~
: minor range. E.g., ~1.2.3 is equivalent to >= 1.2.3, < 1.3.0^
: major range. E.g., ^1.2.3 is equivalent to >= 1.2.3, < 2.0.0[xX*]
: wildcard. E.g., 1.2.x is equivalent to >= 1.2.0, < 1.3.0
branch
: 其對應(yīng)為分支revision
: 其為commit id
-
5.override
:跟constraint
數(shù)據(jù)結(jié)構(gòu)相同铣猩,用來指定傳遞依賴的相關(guān)信息揖铜。
6.metadata
: 項(xiàng)目元數(shù)據(jù),為基本描述信息达皿,其可以存在于root
下天吓,也可以存在于contraint
和override
下,如:
[metadata]
key1 = "value that convey data to other systems"
system1-data = "value that is used by a system"
7. prune
:在其下定義的文件在寫vendor/
時(shí)被廢棄,其可以為全局峦椰,也可以針對特定項(xiàng)目龄寞,其目前可用操作:
unused-packages
indicates that files from directories that do not appear in the package import graph should be pruned.non-go
prunes files that are not used by Go.go-tests
prunes Go test files.
格式如:
[prune]
non-go = true
[[prune.project]]
name = "github.com/project/name"
go-tests = true
non-go = false
Gopkg.lock
在使用dep init
和dep ensure
時(shí)會自動生成