[TOC]
Dep
vendor特性只用來存儲(chǔ)本地依賴
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ù) 需要手動(dòng)修改Gopkg.lock: 定義具體狀態(tài),例如各依賴的revision。自動(dòng)生成的,不可以修改
安裝
直接從官網(wǎng)下載相應(yīng)平臺(tái)的二進(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命令,其會(huì)自動(dòng)下載依賴包,其會(huì)自動(dòng)做以下事情
分析當(dāng)前代碼包中的依賴關(guān)系
將分析出的直接依賴/約束寫到
Gopkg.toml將項(xiàng)目依賴的第三方包,在滿足Gopkg.toml中約束范圍內(nèi)的最新version/branch/revision信息寫入
Gopkg.lock文件中創(chuàng)建
vendor目錄,并以Gopkg.lock為輸入,將其中包下載到vendor下面
如果需手動(dòng)添加一個(gè)具體的依賴,可以使用dep ensure -add xxx
建議直接使用dep ensure,其會(huì)自動(dòng)在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后,其下面會(huì)出現(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,其會(huì)被dep忽略prune: 用于指定哪些文件或目錄不是必需的,其不會(huì)被放入到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-packagesindicates that files from directories that do not appear in the package import graph should be pruned.non-goprunes files that are not used by Go.go-testsprunes 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í)會(huì)自動(dòng)生成