Golang 包管理器 dep 使用介绍。
简介
dep is a dependency management tool for Go. It requires Go 1.9 or newer to compile.
安装
源码
go get -u github.com/golang/dep/cmd/dep
Mac
brew install dep
brew upgrade dep
初始化
dep init
执行后下文件目录如下
tree
# .
# ├── Gopkg.lock # 这个不用管
# ├── Gopkg.toml # 定制三方包规则文件
# └── vendor # 各种三方包文件目录
#
# 1 directory, 2 files
打开Gopkg.toml,开始编辑,例如,要添加一个三方包:
required = ["github.com/gorilla/mux"]
修改后执行下面命令进行更新,注意,此时需要建一个正常编译的go文件,我们这里建立一个main.go,内容如下:
package main
func main() {}
dep ensure
查看vendor目录,需要的包已经安装了
tree vender
# vendor/
# └── github.com
# └── gorilla
# ├── context
# │ ├── context.go
# │ ├── context_test.go
# │ ├── doc.go
# │ ├── LICENSE
# │ └── README.md
# └── mux
# ├── bench_test.go
# ├── context_gorilla.go
# ├── context_gorilla_test.go
# ├── context_native.go
# ├── context_native_test.go
# ├── doc.go
# ├── LICENSE
# ├── mux.go
# ├── mux_test.go
# ├── old_test.go
# ├── README.md
# ├── regexp.go
# └── route.go
#
# 4 directories, 18 files
通常,我们用下面命令查看状态
dep status
# 执行结果如下
# PROJECT CONSTRAINT VERSION REVISION LATEST PKGS USED
# github.com/gorilla/context v1.1 v1.1 1ea2538 1ea2538 1
# github.com/gorilla/mux v1.6.0 v1.6.0 7f08801 7f08801 1
如果我们对三方包有版本要求,比如github.com/gorilla/mux需要1.6.0以下的版本,打开Gopkg.toml,添加如下内容:
# [[constraint]]
# name = "github.com/gorilla/mux"
# version = "<1.6.0"
然后执行:
dep ensure
最终查看状态:
dep status
# PROJECT CONSTRAINT VERSION REVISION LATEST PKGS USED
# github.com/gorilla/context v1.1 v1.1 1ea2538 1ea2538 1
# github.com/gorilla/mux <1.6.0 v1.5.0 24fca30 24fca30 1
最终,项目目录结构如下:
tree
# .
# ├── Gopkg.lock
# ├── Gopkg.toml
# ├── main.go
# └── vendor
# └── github.com
# └── gorilla
# ├── context
# │ ├── context.go
# │ ├── context_test.go
# │ ├── doc.go
# │ ├── LICENSE
# │ └── README.md
# └── mux
# ├── bench_test.go
# ├── context_gorilla.go
# ├── context_gorilla_test.go
# ├── context_native.go
# ├── context_native_test.go
# ├── doc.go
# ├── LICENSE
# ├── mux.go
# ├── mux_test.go
# ├── old_test.go
# ├── README.md
# ├── regexp.go
# └── route.go
#
# 5 directories, 21 files
使用
国内超时
xiexianbin:webhooks xiexianbin$ dep init
Importing configuration from govendor. These are only initial constraints, and are further refined during the solve process.
init failed: unable to solve the dependency graph: Solving failure:
(1) failed to list versions for https://go.googlesource.com/crypto: fatal: unable to access 'https://go.googlesource.com/crypto/': Failed to connect to go.googlesource.com port 443: Operation timed out
: exit status 128
可以采用设置代理的方法解决:
env http_proxy=http://127.0.0.1:8001 https_proxy=http://127.0.0.1:8001 dep init -v
env http_proxy=http://127.0.0.1:8001 https_proxy=http://127.0.0.1:8001 dep ensure -v
类似软件
FAQ
golang dep 导入本地依赖库
目录结构如下:
# cd $GOPATH
# ls src/github.com/x-sso
golang-socials golang-socials-example # 需要引入本地 `github.com/x-sso/golang-socials` 的包
在Gopkg.toml
中添加:
ignored = ["github.com/x-sso/golang-socials/*"]
在go.mod
中添加:
replace github.com/x-sso/golang-socials => ../golang-socials
在golang-socials
目录中执行:
dep ensure -v
go mod vendor
其他引用
对于类似github.com/xiexianbin/go-lib/v1
的引用,在Gopkg.toml
加入ignore,在go.mod中添加对应依赖。