9.5 Go dependency management

9.5 Go dependency management

godep solve package dependencies management tool, the most mainstream one, the principle is scanning information recorded version control.

A. 所有的第三方包都放在$GOPATH的src目录下。 

B. 如果不同程序依赖的版本不一样,怎么管理 

C. 每个程序依赖的包,没有版本号的概念。

godep installation

a. 安装方法: go get github.com/tools/godep
b. 输入godep 命令,出现帮助信息

成功安装后,在$GOPATH的bin目录下会有一个godep可执行的二进制文件,后面执行的命令都是用这个,建议这个目录加入到全局环境变量中。

1.2. Godep usage

1. Looking for a project using third-party package, go standard package does not require management, default backward compatible

如路径 ./src/gostudy/gobook/protobuf
进入protobuf项目

执行命令
godep save

会生成Godeps和vendor文件夹,包含了项目第三方包的版本信息,以及项目依赖的第三方包都放入了vender包了

As Godeps folder, there Godeps.json file

{
    "ImportPath": "gostudy/gobook/protobuf",
    "GoVersion": "go1.10",
    "GodepVersion": "v80",
    "Deps": [
        {
            "ImportPath": "github.com/golang/protobuf/proto",
            "Comment": "v1.3.0-2-gb5d812f",
            "Rev": "b5d812f8a3706043e23a9cd5babf2e5423744d30"//如需要升级,只需要更改这里最新的COMMIT id
        }
    ]
}

Directory Structure Figure

godep workflow

自动扫描当前目录所属包中import的所有外部依赖库(非系统库)

将所有的依赖库下来下来到当前工程中,产生文件 Godeps\Godeps.json 文件

在没有 Godeps\ 文件的情况下,生成模组依赖目录vendor\文件夹

godep save能否成功执行需要有两个要素:

当前或者需扫描的包均能够编译成功:因此所有依赖包事先都应该已经或go get或手工操作保存当前GOPATH路径下

依赖包必须使用了某个代码管理工具(如git,hg):这是因为godep需要记录revision

1.2.1. For pulling restore dependent development

godep restore

It recommends that developers use this command to synchronize the process of dependent libraries

If you download only project  Godeps.json file, but does not include third library you can use godep restore this command down to all dependent libraries $GOPATH\src for developers

godep restoreWhen executed, godepwill follow Godeps/Godeps.jsonwithin the list, sequentially executed go get -d -vto download the corresponding to dependencies GOPATHof the path

1.2.2. Godep command

save     list and copy dependencies into Godeps
go       run the go tool with saved dependencies
get      download and install packages with specified dependencies
path     print GOPATH for dependency code
restore  check out listed dependency versions in GOPATH
update   update selected packages or the go version
diff     shows the diff between current and previously saved set of dependencies
version  show version info

Guess you like

Origin www.cnblogs.com/open-yang/p/11256935.html