dep使用指南

[TOC]

Dep

vendor特性只用來存儲(chǔ)本地依賴

dep為官方支持的實(shí)驗(yàn)性的工具,其官網(wǎng)地址.目前start已經(jīng)9673個(gè)

其通過兩個(gè)metadata文件來管理依賴:

  • Gopkg.toml:定義用戶相關(guān)內(nèi)容,如依賴的source、branchversion等.可以通過命令生產(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),例如各依賴的revision

  • Gopkg.toml:描述用戶的意圖,包括依賴的 source、branch、version等

其關(guān)系如下:

image.png

文件語法

Gopkg.toml語法

  • Dependency rules: constraintsoverrides用于指定依賴的版本以及檢索地址

  • Package graph rules: requiredignored用于包含或排除其相應(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。是constraintversion中的屬性,用來指定依賴的版本。其支持版本比較操作

      • =: 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下,也可以存在于contraintoverride下,如:


[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 initdep ensure時(shí)會(huì)自動(dòng)生成

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • [TOC] 介紹 go dep 依賴管理工具是為應(yīng)用管理代碼的,go get是為GOPATH管理代碼的 官方代碼地...
    木貓尾巴閱讀 21,945評(píng)論 1 13
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,641評(píng)論 19 139
  • pyspark.sql模塊 模塊上下文 Spark SQL和DataFrames的重要類: pyspark.sql...
    mpro閱讀 9,920評(píng)論 0 13
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,740評(píng)論 18 399
  • 這人生最后一次開學(xué),讓我感到不適,但除了平淡的接受別無他選。這一次,還是爸爸送我來,他的白發(fā)多了,皺紋深了...
    本來叫璇宇閱讀 426評(píng)論 0 0

友情鏈接更多精彩內(nèi)容