Post

golang 包管理

godep

安装

go get -u -v github.com/tools/godep

使用

  1. 参考 1
  2. 参考 2

goverdor(go1.12 下 推荐使用)

安装

go get -u github.com/kardianos/govendor

基本使用

进入项目目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
govendor init
govendor fetch github.com/Cc360428/HelpPackage/UtilsHelp
cat vendor/vendor.json
{
        "comment": "",
        "ignore": "test",
        "package": [
                {
                        "checksumSHA1": "vT6zp7A2xIrOTza5mYU7ZR4it1c=",
                        "path": "github.com/Cc360428/HelpPackage",
                        "revision": "8c482c1a5d4c81ee28409d5efcc557b3fa1b89c2",
                        "revisionTime": "2020-03-05T05:01:58Z"
                },
                {
                        "path": "github.com/Cc360428/HelpPackage/UtilsHelp",
                        "revision": ""
                }
        ],
        "rootPath": "Cc360428/cloud"
}

更多使用

govendor –help

  1. 依赖更新
  2. 移除依赖
  3. 查看依赖
  4. 依赖丢失

缺点

必须在 gopath 目录下使用

go mod

注:golang 版本升级到 1.11(建议 1.12)

设置

注: GO111MODULE 有三个值(off、on、auto)

windows

1
2
 setx GO111MODULE on
 setx GOPROXY https://goproxy.io

linux

1
2
export GO111MODULE=on
export GOPROXY=https://goproxy.io

使用

进入项目根目录下

  • 初始化

go mod init [mod 名字]

  • 拉取缺少模块

go mod tidy

  • 下载依赖包

go mod download

  • 编辑 go.mod

go mod edit

  • 打印模块依赖图

go mod graph

  • 将依赖复制到 vendor

vendor

  • 验证依赖是否正确

verify

  • 解释为什么需要依赖

why

go mod help

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
$ go help mod
Go mod provides access to operations on modules.

Note that support for modules is built into all the go commands,
not just 'go mod'. For example, day-to-day adding, removing, upgrading,
and downgrading of dependencies should be done using 'go get'.
See 'go help modules' for an overview of module functionality.

Usage:

        go mod <command> [arguments]

The commands are:

        download    download modules to local cache
        edit        edit go.mod from tools or scripts
        graph       print module requirement graph
        init        initialize new module in current directory
        tidy        add missing and remove unused modules
        vendor      make vendored copy of dependencies
        verify      verify dependencies have expected content
        why         explain why packages or modules are needed

Use "go help mod <command>" for more information about a command.
This post is licensed under CC BY 4.0 by the author.