Go Go module dependency management and use

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 vendormode, if there is vendor directory under the project directory, then go tool chain will give priority to the use of vendorthe package in the compiling, testing and so on.

godepGo 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 godeptool.

go get github.com/tools/godep

Basic Commands

After installing godep, enter in a terminal godepto 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 savecommand is created in the current project Godepsand vendertwo folders.

Wherein the Godepsfolder has a Godeps.jsonfile, which records the packet information items depends. venderFolder 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 venderfile folder to find, if not found it will go to $GOAPTH/srcfind the directory.

godep development process

  1. Assurance procedures to compile the normal
  2. Execution godep saveto save the current project of all third-party information and code dependent version
  3. Godeps directories and directory submission to the vender code base.
  4. If you want to update the version-dependent, you can directly modify Godeps.jsonthe corresponding entry in the file

go module

go moduleAfter Go1.11 version is the official launch of version management tools, and from Go1.13 version, go modulewill be the default language Go dependency management tool.

GO111MODULE

To enable go modulesupport must first set environment variables GO111MODULE, which can be turned on or off via the module support, it has three possible values: off, , on, autodefault is auto.

  1. GO111MODULE=offDisable module support, it will be from the compile-time GOPATHand vendorfind the package folder.
  2. GO111MODULE=onEnabling module supports ignored when compiling GOPATHand vendorfolders, based only go.moddownload the dependencies.
  3. GO111MODULE=autoWhen the project $GOPATH/srcoutside the project root directory and go.moda file, open the module supports.

In simple terms, set up GO111MODULE=onafter 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.modand go.sum.

GOPROXY

Go1.11 set GOPROXY command after:

export GOPROXY=https://goproxy.cn

Go1.13 after the GOPROXYdefault value https://proxy.golang.orgin 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 modcommands 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 &lt;=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 =&gt; github.com/golang/crypto v0.0.0-20180820150726-614d502a4dac
    golang.org/x/net v0.0.0-20180821023952-922f4815f713 =&gt; github.com/golang/net v0.0.0-20180826012351-8a410e7b638d
    golang.org/x/text v0.3.0 =&gt; github.com/golang/text v0.3.0
)

go get

在项目中执行go get命令可以下载依赖包,并且还可以指定下载的版本。

  1. 运行go get -u将会升级到最新的次要版本或者修订版本(x.y.z, z是修订版本号, y是次要版本号)
  2. 运行go get -u=patch将会升级到最新的修订版本
  3. 运行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,可以按照以下步骤操作:

  1. 在项目目录下执行go mod init,生成一个go.mod文件。
  2. 执行go get,查找并记录当前项目的依赖,同时生成一个go.sum记录每个依赖库的版本和哈希值。

新项目

对于一个新创建的项目,我们可以在项目文件夹下按照以下步骤操作:

  1. 执行go mod init 项目名命令,在当前项目文件夹下创建一个go.mod文件。
  2. 手动编辑go.mod中的require依赖项或执行go get自动发现、维护依赖。

Guess you like

Origin www.cnblogs.com/nickchen121/p/11517437.html