Home Archives Categories Tags Docs

Golang 包管理器

发布时间: 更新时间: 总字数:747 阅读时间:2m 作者: 分享

Golang 包管理器

简介

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

可以采用设置代理的方法解决:

export http_proxy=localhost:1080

类似软件

  • glide

参考