dep使用指南

[TOC]

Dep

vendor特性只用來存儲本地依賴

dep為官方支持的實驗性的工具,其官網地址.目前start已經9673

其通過兩個metadata文件來管理依賴:

  • Gopkg.toml:定義用戶相關內容,如依賴的sourcebranchversion等.可以通過命令生產,也可以被用戶根據 需要手動修改

  • Gopkg.lock: 定義具體狀態,例如各依賴的revision。自動生成的,不可以修改

安裝

直接從官網下載相應平臺的二進制包,放入到環境變量中即可(為了使用更方便,可以進行重命名一下)

使用

使用流程:

1. 創建項目

2. 執行dep init進行初始化

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命令,其會自動下載依賴包,其會自動做以下事情

  • 分析當前代碼包中的依賴關系

  • 將分析出的直接依賴/約束寫到Gopkg.toml

  • 將項目依賴的第三方包,在滿足Gopkg.toml中約束范圍內的最新version/branch/revision信息寫入Gopkg.lock文件中

  • 創建vendor目錄,并以Gopkg.lock為輸入,將其中包下載到vendor下面



如果需手動添加一個具體的依賴,可以使用dep ensure -add xxx

建議直接使用dep ensure,其會自動在vendor目錄中增加或更新相關依賴(add/update/remove)


查看狀態:dep status


其緩存是放在$GOPATH/pkg/dep/sources里面

Dep通過兩個metadata文件來管理依賴: manifest文件Gopkg.toml和lock文件Gopkg.lock

$GOPATH/src下創建項目目錄,如foo,執行dep init后,其下面會出現以下文件或者目錄

  • vendor:目錄

  • Gopkg.lock:描述依賴的具體狀態,例如各依賴的revision

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

其關系如下:

image.png

文件語法

Gopkg.toml語法

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

  • Package graph rules: requiredignored用于包含或排除其相應的包

  • metadata:用戶定義的鍵值對map,其會被dep忽略

  • prune: 用于指定哪些文件或目錄不是必需的,其不會被放入到vendor

1.注釋: 使用#

2.required:是一個包(不是projects)的列表,該包具有以下特性;

  • 被項目用到

  • 沒有被直接或者傳遞import

  • 不想被加入到GOPAT中,也不想lock它的版本

其格式如下:


required = ["github.com/user/thing/cmd/thing"]

3.ignored:是一個包(不是projects)的列表,避免某些package加入到依賴中,格式如下:


ignored = ["github.com/user/project/badpkg"]

4.constraint: 指定直接依賴的相關信息。其格式如下


[[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: 指定依賴的項目地址

  • version|branch|revision: 指定依賴的版本,這三者只能同時存在一個

    • version:其對應為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: 其對應為分支

    • revision: 其為commit id

5.override:跟constraint數據結構相同,用來指定傳遞依賴的相關信息。

6.metadata: 項目元數據,為基本描述信息,其可以存在于root下,也可以存在于contraintoverride下,如:


[metadata]

key1 = "value that convey data to other systems"

system1-data = "value that is used by a system"

7. prune:在其下定義的文件在寫vendor/時被廢棄,其可以為全局,也可以針對特定項目,其目前可用操作:

  • 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時會自動生成

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • [TOC] 介紹 go dep 依賴管理工具是為應用管理代碼的,go get是為GOPATH管理代碼的 官方代碼地...
    木貓尾巴閱讀 21,822評論 1 13
  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,837評論 18 139
  • pyspark.sql模塊 模塊上下文 Spark SQL和DataFrames的重要類: pyspark.sql...
    mpro閱讀 9,487評論 0 13
  • 1. Java基礎部分 基礎部分的順序:基本語法,類相關的語法,內部類的語法,繼承相關的語法,異常的語法,線程的語...
    子非魚_t_閱讀 31,739評論 18 399
  • 這人生最后一次開學,讓我感到不適,但除了平淡的接受別無他選。這一次,還是爸爸送我來,他的白發多了,皺紋深了...
    本來叫璇宇閱讀 399評論 0 0