Go依賴管理工具 - dep

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

Go依賴管理工具

Go dependency management tool

環境要求

  • Golang >= 1.9
  • Dep

目前版本:

dep: version     : devel build date  :  git hash    :  go version  : go1.10 go compiler : gc platform    : linux/amd64

Latest releasev0.4.1

安裝

go get -u github.com/golang/dep/cmd/dep

$GOPATH/bin不在PATH下,則需要將產生的dep檔案從$GOPATH/bin移動至$GOBIAN

驗證

$ depDep is a tool for managing dependencies for Go projectsUsage: "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 informationExamples:  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 projectUse "dep help [command]" for more information about a command.

初始化

在項目根目錄執行初始化命令,dep在初始化時會分析應用程式所需要的所有依賴包,得出依賴包清單

並產生vendor目錄,Gopkg.tomlGopkg.lock檔案

預設初始化

$ dep init -v

直接從對應網路資源處下載

優先從$GOPATH初始化

$ dep init -gopath -v

該命令會先從$GOPATH尋找既有的依賴包,若不存在則從對應網路資源處下載

Gopkg.toml

該檔案由dep init產生,包含管理dep行為的規則聲明

required = ["github.com/user/thing/cmd/thing"]ignored = [  "github.com/user/project/pkgX",  "bitbucket.org/user/project/pkgA/pkgY"][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"[[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"

Gopkg.lock

該檔案由dep ensuredep init產生,包含一個項目依賴關係圖的傳遞完整快照,表示為一系列[[project]]

# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.[[projects]]  branch = "master"  name = "github.com/golang/protobuf"  packages = [    "jsonpb",    "proto",    "protoc-gen-go/descriptor",    "ptypes",    "ptypes/any",    "ptypes/duration",    "ptypes/struct",    "ptypes/timestamp"  ]  revision = "bbd03ef6da3a115852eaf24c8a1c46aeb39aa175"

常用命令

dep ensure

從項目中的Gopkg.tomlGopkg.lock中分析關係圖,並擷取所需的依賴包

用於確保本地的關係圖、鎖、依賴包清單完全一致

dep ensure -add

# 引入該依賴包的最新版本dep ensure -add github.com/pkg/foo# 引入具有特定約束(指定版本)的依賴包dep ensure -add github.com/pkg/foo@^1.0.1

dep ensure -update

Gopkg.lock中的約定依賴項更新為Gopkg.toml允許的最新版本

最後

目前dep還在官方實驗階段,但已表示生產可安全使用

如果出現什麼問題,大家可以一起留個言討論討論

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.