2.1 Go依赖管理工具dep

Go dependency management tool

环境要求

  • Golang >= 1.9
  • Dep

目前版本:

  1. dep:
  2. version : devel
  3. build date :
  4. git hash :
  5. go version : go1.10
  6. go compiler : gc
  7. platform : linux/amd64

Latest releasev0.4.1

安装

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

$GOPATH/bin不在PATH下,则需要将生成的dep文件从$GOPATH/bin移动至$GOBIAN

验证

  1. $ dep
  2. Dep is a tool for managing dependencies for Go projects
  3. Usage: "dep [command]"
  4. Commands:
  5. init Set up a new Go project, or migrate an existing one
  6. status Report the status of the project's dependencies
  7. ensure Ensure a dependency is safely vendored in the project
  8. prune Pruning is now performed automatically by dep ensure.
  9. version Show the dep version information
  10. Examples:
  11. dep init set up a new project
  12. dep ensure install the project's dependencies
  13. dep ensure -update update the locked versions of all dependencies
  14. dep ensure -add github.com/pkg/errors add a dependency to the project
  15. Use "dep help [command]" for more information about a command.

初始化

在项目根目录执行初始化命令,dep在初始化时会分析应用程序所需要的所有依赖包,得出依赖包清单

并生成vendor目录,Gopkg.tomlGopkg.lock文件

image

默认初始化

  1. $ dep init -v

直接从对应网络资源处下载

优先从$GOPATH初始化

  1. $ dep init -gopath -v

该命令会先从$GOPATH查找既有的依赖包,若不存在则从对应网络资源处下载

Gopkg.toml

该文件由dep init生成,包含管理dep行为的规则声明

  1. required = ["github.com/user/thing/cmd/thing"]
  2. ignored = [
  3. "github.com/user/project/pkgX",
  4. "bitbucket.org/user/project/pkgA/pkgY"
  5. ]
  6. [metadata]
  7. key1 = "value that convey data to other systems"
  8. system1-data = "value that is used by a system"
  9. system2-data = "value that is used by another system"
  10. [[constraint]]
  11. # Required: the root import path of the project being constrained.
  12. name = "github.com/user/project"
  13. # Recommended: the version constraint to enforce for the project.
  14. # Note that only one of "branch", "version" or "revision" can be specified.
  15. version = "1.0.0"
  16. branch = "master"
  17. revision = "abc123"
  18. # Optional: an alternate location (URL or import path) for the project's source.
  19. source = "https://github.com/myfork/package.git"
  20. # Optional: metadata about the constraint or override that could be used by other independent systems
  21. [metadata]
  22. key1 = "value that convey data to other systems"
  23. system1-data = "value that is used by a system"
  24. system2-data = "value that is used by another system"

Gopkg.lock

该文件由dep ensuredep init生成,包含一个项目依赖关系图的传递完整快照,表示为一系列[[project]]

  1. # This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
  2. [[projects]]
  3. branch = "master"
  4. name = "github.com/golang/protobuf"
  5. packages = [
  6. "jsonpb",
  7. "proto",
  8. "protoc-gen-go/descriptor",
  9. "ptypes",
  10. "ptypes/any",
  11. "ptypes/duration",
  12. "ptypes/struct",
  13. "ptypes/timestamp"
  14. ]
  15. revision = "bbd03ef6da3a115852eaf24c8a1c46aeb39aa175"

常用命令

dep ensure

从项目中的Gopkg.tomlGopkg.lock中分析关系图,并获取所需的依赖包

用于确保本地的关系图、锁、依赖包清单完全一致

dep ensure -add

  1. # 引入该依赖包的最新版本
  2. dep ensure -add github.com/pkg/foo
  3. # 引入具有特定约束(指定版本)的依赖包
  4. dep ensure -add github.com/pkg/foo@^1.0.1

dep ensure -update

Gopkg.lock中的约定依赖项更新为Gopkg.toml允许的最新版本

最后

目前dep还在官方试验阶段,但已表示生产可安全使用

如果出现什么问题,大家可以一起留个言讨论讨论