Go dependency management with the version of the change of language is gradually perfected.
Dependency Management
Why rely on Management
The first time, Go all rely on third-party libraries are placed GOPATH this directory. This leads to the same library can store only one version of the code. If the different projects depend on third-party libraries with a different version of how to solve?
godep
Go v1.5 language from the beginning started to introduce vendor
mode, if there is vendor directory under the project directory, then go tool chain will give priority to the use of vendor
the package in the compiling, testing and so on.
godep
Go language is a third-party dependency management tool, maintained by the community as well as a similar quasi-official package management tools implemented by a vender mode dep
.
installation
Execute the following command to install the godep
tool.
go get github.com/tools/godep
Basic Commands
After installing godep, enter in a terminal godep
to see all the commands supported.
godep save 将依赖项输出并复制到Godeps.json文件中
godep go 使用保存的依赖项运行go工具
godep get 下载并安装具有指定依赖项的包
godep path 打印依赖的GOPATH路径
godep restore 在GOPATH中拉取依赖的版本
godep update 更新选定的包或go版本
godep diff 显示当前和以前保存的依赖项集之间的差异
godep version 查看版本信息
Use godep help [command]
can look at the specific command help information.
Use godep
In the implementation of the project directory godep save
command is created in the current project Godeps
and vender
two folders.
Wherein the Godeps
folder has a Godeps.json
file, which records the packet information items depends. vender
Folder is the project dependent on the source code files package.
vender mechanism
After Go1.5 version began to support, be able to control the Go language compiler program depends upon packet priority search path.
For example, a project dependencies to find the first project will be in the root directory of the vender
file folder to find, if not found it will go to $GOAPTH/src
find the directory.
godep development process
- Assurance procedures to compile the normal
- Execution
godep save
to save the current project of all third-party information and code dependent version - Godeps directories and directory submission to the vender code base.
- If you want to update the version-dependent, you can directly modify
Godeps.json
the corresponding entry in the file
go module
go module
After Go1.11 version is the official launch of version management tools, and from Go1.13 version, go module
will be the default language Go dependency management tool.
GO111MODULE
To enable go module
support must first set environment variables GO111MODULE
, which can be turned on or off via the module support, it has three possible values: off
, , on
, auto
default is auto
.
GO111MODULE=off
Disable module support, it will be from the compile-timeGOPATH
andvendor
find the package folder.GO111MODULE=on
Enabling module supports ignored when compilingGOPATH
andvendor
folders, based onlygo.mod
download the dependencies.GO111MODULE=auto
When the project$GOPATH/src
outside the project root directory andgo.mod
a file, open the module supports.
In simple terms, set up GO111MODULE=on
after which you can use go module
, and since there is no need to create a project in GOPATH in, and can also be well managed project depends third-party package information.
After using the go module management relies generates two files in the root directory of the project go.mod
and go.sum
.
GOPROXY
Go1.11 set GOPROXY command after:
export GOPROXY=https://goproxy.cn
Go1.13 after the GOPROXY
default value https://proxy.golang.org
in the country is not accessible, it is recommended that you set GOPROXY, I recommend using goproxy.cn .
go env -w GOPROXY=https://goproxy.cn,direct
go mod command
Commonly used go mod
commands are as follows:
go mod download 下载依赖的module到本地cache(默认为$GOPATH/pkg/mod目录)
go mod edit 编辑go.mod文件
go mod graph 打印模块依赖图
go mod init 初始化当前文件夹, 创建go.mod文件
go mod tidy 增加缺少的module,删除无用的module
go mod vendor 将依赖复制到vendor下
go mod verify 校验依赖
go mod why 解释为什么需要依赖
<h3 id="go-mod">go.mod</h3> <p>go.mod文件记录了项目所有的依赖信息,其结构大致如下:</p> <pre><code class="language-sh">module github.com/Q1mi/studygo/blogger go 1.12 require ( github.com/DeanThompson/ginpprof v0.0.0-20190408063150-3be636683586 github.com/gin-gonic/gin v1.4.0 github.com/go-sql-driver/mysql v1.4.1 github.com/jmoiron/sqlx v1.2.0 github.com/satori/go.uuid v1.2.0 google.golang.org/appengine v1.6.1 // indirect )
其中,
module
用来定义包名require
用来定义依赖包及版本indirect
表示间接引用依赖的版本
go mod支持语义化版本号,比如
go get [email protected]
,也可以跟git的分支或tag,比如go get foo@master
,当然也可以跟git提交哈希,比如go get foo@e3702bed2
。关于依赖的版本支持以下几种格式:gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 gopkg.in/vmihailenco/msgpack.v2 v2.9.1 gopkg.in/yaml.v2 <=v2.2.1 github.com/tatsushid/go-fastping v0.0.0-20160109021039-d7bb493dee3e latest
replace
在国内访问golang.org/x的各个包都需要翻墙,你可以在go.mod中使用replace替换成github上对应的库。
replace ( golang.org/x/crypto v0.0.0-20180820150726-614d502a4dac => github.com/golang/crypto v0.0.0-20180820150726-614d502a4dac golang.org/x/net v0.0.0-20180821023952-922f4815f713 => github.com/golang/net v0.0.0-20180826012351-8a410e7b638d golang.org/x/text v0.3.0 => github.com/golang/text v0.3.0 )
go get
在项目中执行
go get
命令可以下载依赖包,并且还可以指定下载的版本。
- 运行
go get -u
将会升级到最新的次要版本或者修订版本(x.y.z, z是修订版本号, y是次要版本号)- 运行
go get -u=patch
将会升级到最新的修订版本- 运行
go get package@version
将会升级到指定的版本号version如果下载所有依赖可以使用
go mod download
命令。整理依赖
我们在代码中删除依赖代码后,相关的依赖库并不会在
go.mod
文件中自动移除。这种情况下我们可以使用go mod tidy
命令更新go.mod
中的依赖关系。go mod edit
格式化
因为我们可以手动修改go.mod文件,所以有些时候需要格式化该文件。Go提供了一下命令:
go mod edit -fmt
添加依赖项
go mod edit -require=golang.org/x/text
移除依赖项
如果只是想修改
go.mod
文件中的内容,那么可以运行go mod edit -droprequire=package path
,比如要在go.mod
中移除golang.org/x/text
包,可以使用如下命令:go mod edit -droprequire=golang.org/x/text
关于
go mod edit
的更多用法可以通过go help mod edit
查看。在项目中使用go module
既有项目
如果需要对一个已经存在的项目启用
go module
,可以按照以下步骤操作:
- 在项目目录下执行
go mod init
,生成一个go.mod
文件。- 执行
go get
,查找并记录当前项目的依赖,同时生成一个go.sum
记录每个依赖库的版本和哈希值。新项目
对于一个新创建的项目,我们可以在项目文件夹下按照以下步骤操作:
- 执行
go mod init 项目名
命令,在当前项目文件夹下创建一个go.mod
文件。- 手动编辑
go.mod
中的require依赖项或执行go get
自动发现、维护依赖。