Go 包管理工具 dep 安装与使用
必须认识到的问题
国内使用 dep 可能同步包会很慢或直接失败,所以我们要设置代理
已有更好的包管理工具 Go Module
新手请直接学习该工具,点击这里,但如果想试试 dep,本文可作为参考。
第一步:确保网络通畅
- 确保您已拥有十八般武艺,能够自由穿梭 internet
- 让
iTerm
支持使curl
,wget
,brew
等 应用程序 会调用 http_proxy 和 https_proxy 这两环境变量进行代 -
Mac 环境下 iTerm 具体操作:
-
安装privoxy,作用:使请求转为http请求从而使用 http代理
brew install privoxy
-
设置 http代理
export http_proxy=http://127.0.0.1:8087 export https_proxy=$http_proxy
-
- 配置privoxy
vim /usr/local/etc/privoxy/config # 写入 listen-address 127.0.0.1:8087 forward-socks5 / 127.0.0.1:1080 . forward 192.168.*.*/ . forward 10.*.*.*/ . forward 127.*.*.*/ #保存并启动privoxy brew services start privoxy
- 代理是否生效,可使用
curl ip.gs
,如:curl ip.gs ... Current IP / 当前 IP: 45.**.**.*** ISP / 运营商: linode.com City / 城市: Dallas Texas Country / 国家: United States # 现在使用的美国IP ...
以上是我的实践过程,您的实践过程可能会遇到一些问题,别怕,我也是一个一个坑过来的。
第二步:安装 dep
- brew install dep
brew install dep
第三步:使用
如果您之前未使用任何包管理工具,那太好了,不会受其它 包管理思想 影响,如果有其它包使用经验 比如
npm
||composer
,那太好了,可以研究对比下不同思想。
- 创建一个项目
$ mkdir -p $GOPATH/src/github.com/me/example $ cd $GOPATH/src/github.com/me/example
- 将初始化项目
$ dep init $ ls Gopkg.toml Gopkg.lock vendor/
- 各个文件的作用
- Gopkg.toml 用于调整Gopkg.lock已同步的一些包,如版本高了,在这个文件设置对应版本,运行命令后,就同步到Gopkg.lock。 有其它包管理工具的注意了 跟
composer
的 composer.json 和npm
的 package.json 不一样,它不是主要依赖次文件同步管理包,这个文件只是作为 调整,调整,调整!相信您已经记住了。 - Gopkg.lock 根据项目代码里面的
import
和 Gopkg.toml文件,获取相关依赖,最后写入到 项目 vendor/目录下。
- Gopkg.toml 用于调整Gopkg.lock已同步的一些包,如版本高了,在这个文件设置对应版本,运行命令后,就同步到Gopkg.lock。 有其它包管理工具的注意了 跟
第四步:思想,这是最重要的内容
Go 语言讲求没用到的东西,就不应该存在在代码里面,所以 dep 包管理工具和其它语言的包管理工具不一样,它根据您项目里用到什么第三方包,来生成对应的依赖配置文件Gopkg.lock,如果配置文件的软件包存在问题,则通过Gopkg.toml文件调整,比如第三方软件包版本高了,需要指定某个版本,那么在Gopkg.toml文件写入相关配置,然后 通过
dep ensure -update -v
更新即可,加-v
可以看到运行过程信息。
第五步:命令
dep ensure
同步Gopkg.toml文件修改的配置,加上-update
则是即同步也更新各个依赖。dep ensure -add github.com/XXX/XX
添加新依赖项,比如官方文档的例子:$ dep ensure -add github.com/pkg/errors
成功的话,会更新Gopkg.lock文件和vendor/目录,并会写入配置约束github.com/pkg/errors到Gopkg.toml文件。如果你代码里面没有用这个第三方依赖包(import),它还会报告一个警告:
"github.com/pkg/errors" is not imported by your project, and has been temporarily added to Gopkg.lock and vendor/. If you run "dep ensure" again before actually importing it, it will disappear from Gopkg.lock and vendor/.
所以,我觉得没有必要通过这命令来添加依赖,直接在代码里面
import github.com/pkg/errors
,再运行dep ensure
岂不美哉?!- 当之前用过的依赖包,后面代码没再使用过,运行
dep ensure [-update]
会删除在Gopkg.lock的配置和vendor/里面的文件,反之 新添加的包运行后,则会添加到Gopkg.lock的配置和vendor/里面。
第六步:Gopkg.toml 可用配置项
required
,这等同.go
文件中的import
于语句ignored
,我的理解是忽略某些依赖包[[constraint]]
,定义指定依赖的配置规则[[override]]
,等同于[[constraint]]
,但它的作用是调整某些依赖包,比如版本下调。[prune]
,用于管理应从中删除的文件类型vendor/
例:我使用 iris 框架写 web 项目,结果 iris 包的依赖包升级后出现写不兼容bug,我需要调整依赖包版本,于是我的 Gopkg.toml文件如下:
...
[[constraint]]
name = "github.com/kataras/iris"
version = "11.1.0"
[[override]]
name = "github.com/flosch/pongo2"
branch = "master" # master 分支已修复不兼容bug
...
其它资料
官方文档,有条件的朋友最好仔细看看该文档。
使用它的样子
#查看当前依赖
$ dep status
Gopkg.lock is out of sync with imports and/or Gopkg.toml. Run `dep check` for details.
PROJECT MISSING PACKAGES
input-digest mismatch
#排查问题
$ dep check
# Gopkg.lock is out of sync:
github.com/kataras/iris: in Gopkg.lock s input-imports, but neither imported nor required
#同步更新
$ dep ensure -update -v
Warning: the following project(s) have [[constraint]] stanzas in Gopkg.toml:
✗ github.com/kataras/iris
✗ github.com/pkg/errors
However, these projects are not direct dependencies of the current project:
they are not imported in any .go files, nor are they in the 'required' list in
Gopkg.toml. Dep only applies [[constraint]] rules to direct dependencies, so
these rules will have no effect.
Either import/require packages from these projects so that they become direct
dependencies, or convert each [[constraint]] to an [[override]] to enforce rules
on these projects, if they happen to be transitive dependencies.
Root project is "chuizi"
1 transitively valid internal packages
1 external packages imported from 1 projects
(0) ✓ select (root)
(1) ? attempt github.com/dgrijalva/jwt-go with 1 pkgs; 20 versions to try
(1) try github.com/dgrijalva/jwt-go@v3.2.0
(2) ✗ github.com/dgrijalva/jwt-go@v3.2.0 not allowed by constraint master:
...
(1) try github.com/dgrijalva/jwt-go@master
(1) ✓ select github.com/dgrijalva/jwt-go@master w/1 pkgs
✓ found solution with 1 packages from 1 projects
Solver wall times by segment:
b-source-exists: 1.506033451s
b-list-versions: 1.199125146s
b-list-pkgs: 168.073552ms
b-gmal: 146.433008ms
satisfy: 578.332µs
new-atom: 307.683µs
select-root: 86.261µs
select-atom: 78.903µs
other: 13.765µs
TOTAL: 3.020730101s
#再查看当前依赖状态
$ dep status
PROJECT CONSTRAINT VERSION REVISION LATEST PKGS USED
github.com/dgrijalva/jwt-go branch master branch master 5e25c22 5e25c22 1
本作品采用《CC 协议》,转载必须注明作者和本文链接
建議現在新人進入 Golang 還是以 Go Module 為主