|
| 1 | +# Go依赖管理工具 |
| 2 | +>Go dependency management tool |
| 3 | +## 环境要求 |
| 4 | +- Golang >= 1.9 |
| 5 | +- Dep |
| 6 | + |
| 7 | +## 目前版本: |
| 8 | +``` |
| 9 | +dep: |
| 10 | + version : devel |
| 11 | + build date : |
| 12 | + git hash : |
| 13 | + go version : go1.10 |
| 14 | + go compiler : gc |
| 15 | + platform : linux/amd64 |
| 16 | +``` |
| 17 | +`Latest release`为`v0.4.1` |
| 18 | + |
| 19 | +## 安装 |
| 20 | +``` |
| 21 | +go get -u github.com/golang/dep/cmd/dep |
| 22 | +``` |
| 23 | +若`$GOPATH/bin`不在`PATH`下,则需要将生成的`dep`文件从`$GOPATH/bin`移动至`$GOBIAN`下 |
| 24 | + |
| 25 | +## 验证 |
| 26 | +``` |
| 27 | +$ dep |
| 28 | +Dep is a tool for managing dependencies for Go projects |
| 29 | +
|
| 30 | +Usage: "dep [command]" |
| 31 | +
|
| 32 | +Commands: |
| 33 | +
|
| 34 | + init Set up a new Go project, or migrate an existing one |
| 35 | + status Report the status of the project's dependencies |
| 36 | + ensure Ensure a dependency is safely vendored in the project |
| 37 | + prune Pruning is now performed automatically by dep ensure. |
| 38 | + version Show the dep version information |
| 39 | +
|
| 40 | +Examples: |
| 41 | + dep init set up a new project |
| 42 | + dep ensure install the project's dependencies |
| 43 | + dep ensure -update update the locked versions of all dependencies |
| 44 | + dep ensure -add github.com/pkg/errors add a dependency to the project |
| 45 | +
|
| 46 | +Use "dep help [command]" for more information about a command. |
| 47 | +
|
| 48 | +``` |
| 49 | + |
| 50 | +## 初始化 |
| 51 | +在项目根目录执行初始化命令,`dep`在初始化时会分析应用程序所需要的所有依赖包,得出依赖包清单 |
| 52 | + |
| 53 | +并生成`vendor`目录,`Gopkg.toml`、`Gopkg.lock`文件 |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | +### 默认初始化 |
| 58 | +``` |
| 59 | +$ dep init -v |
| 60 | +``` |
| 61 | +直接从对应网络资源处下载 |
| 62 | + |
| 63 | +### 优先从$GOPATH初始化 |
| 64 | +``` |
| 65 | +$ dep init -gopath -v |
| 66 | +``` |
| 67 | +该命令会先从`$GOPATH`查找既有的依赖包,若不存在则从对应网络资源处下载 |
| 68 | + |
| 69 | +### Gopkg.toml |
| 70 | + |
| 71 | +该文件由`dep init`生成,包含管理`dep`行为的规则声明 |
| 72 | + |
| 73 | +``` |
| 74 | +required = ["github.com/user/thing/cmd/thing"] |
| 75 | +
|
| 76 | +ignored = [ |
| 77 | + "github.com/user/project/pkgX", |
| 78 | + "bitbucket.org/user/project/pkgA/pkgY" |
| 79 | +] |
| 80 | +
|
| 81 | +[metadata] |
| 82 | +key1 = "value that convey data to other systems" |
| 83 | +system1-data = "value that is used by a system" |
| 84 | +system2-data = "value that is used by another system" |
| 85 | +
|
| 86 | +[[constraint]] |
| 87 | + # Required: the root import path of the project being constrained. |
| 88 | + name = "github.com/user/project" |
| 89 | + # Recommended: the version constraint to enforce for the project. |
| 90 | + # Note that only one of "branch", "version" or "revision" can be specified. |
| 91 | + version = "1.0.0" |
| 92 | + branch = "master" |
| 93 | + revision = "abc123" |
| 94 | +
|
| 95 | + # Optional: an alternate location (URL or import path) for the project's source. |
| 96 | + source = "https://github.com/myfork/package.git" |
| 97 | +
|
| 98 | + # Optional: metadata about the constraint or override that could be used by other independent systems |
| 99 | + [metadata] |
| 100 | + key1 = "value that convey data to other systems" |
| 101 | + system1-data = "value that is used by a system" |
| 102 | + system2-data = "value that is used by another system" |
| 103 | +``` |
| 104 | + |
| 105 | +### Gopkg.lock |
| 106 | + |
| 107 | +该文件由`dep ensure`和`dep init`生成,包含一个项目依赖关系图的传递完整快照,表示为一系列`[[project]]`节 |
| 108 | + |
| 109 | +``` |
| 110 | +# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. |
| 111 | +
|
| 112 | +[[projects]] |
| 113 | + branch = "master" |
| 114 | + name = "github.com/golang/protobuf" |
| 115 | + packages = [ |
| 116 | + "jsonpb", |
| 117 | + "proto", |
| 118 | + "protoc-gen-go/descriptor", |
| 119 | + "ptypes", |
| 120 | + "ptypes/any", |
| 121 | + "ptypes/duration", |
| 122 | + "ptypes/struct", |
| 123 | + "ptypes/timestamp" |
| 124 | + ] |
| 125 | + revision = "bbd03ef6da3a115852eaf24c8a1c46aeb39aa175" |
| 126 | +``` |
| 127 | + |
| 128 | +## 常用命令 |
| 129 | + |
| 130 | +### dep ensure |
| 131 | +从项目中的`Gopkg.toml`和`Gopkg.lock`中分析关系图,并获取所需的依赖包 |
| 132 | + |
| 133 | +用于确保本地的关系图、锁、依赖包清单完全一致 |
| 134 | + |
| 135 | +### dep ensure -add |
| 136 | + |
| 137 | +``` |
| 138 | +# 引入该依赖包的最新版本 |
| 139 | +dep ensure -add github.com/pkg/foo |
| 140 | +
|
| 141 | +# 引入具有特定约束(指定版本)的依赖包 |
| 142 | +dep ensure -add github.com/pkg/foo@^1.0.1 |
| 143 | +``` |
| 144 | + |
| 145 | +### dep ensure -update |
| 146 | +将`Gopkg.lock`中的约定依赖项更新为`Gopkg.toml`允许的最新版本 |
| 147 | + |
| 148 | +## 最后 |
| 149 | + |
| 150 | +目前`dep`还在官方试验阶段,但已表示生产可安全使用 |
| 151 | + |
| 152 | +如果出现什么问题,大家可以一起留个言讨论讨论 |
0 commit comments