继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

go包管理器

慕哥9229398
关注TA
已关注
手记 1099
粉丝 198
获赞 911

webp

包管理器

如果使用过java和python的包管理,一定对包管理很熟悉。通过包管理可以提高开发效率,把时间放在技术的提升,而不是代码的管理和同步上。

govendor简介

github

golang工程的依赖包经常使用go get命令来获取,例如:go get github.com/kardianos/govendor ,会将依赖包下载到GOPATH的路径下。

常用的依赖包管理工具有godep,govendor等,在Golang1.5之后,Go提供了 GO15VENDOREXPERIMENT 环境变量(Go 1.6版本默认开启该环境变量),用于将go build时的应用路径搜索调整成为 当前项目目录/vendor 目录方式。通过这种形式,我们可以实现类似于 godep 方式的项目依赖管理。

长期以来,golang 对外部依赖都没有很好的管理方式,只能从 $GOPATH 下查找依赖。这就造成不同用户在安装同一个项目适合可能从外部获取到不同的依赖库版本,同时当无法联网时,无法编译依赖缺失的项目。

自 1.5 版本开始引入 govendor 工具,该工具将项目依赖的外部包放到项目下的 vendor 目录下(对比 nodejs 的 node_modules 目录),并通过 vendor.json 文件来记录依赖包的版本,方便用户使用相对稳定的依赖。

对于 govendor 来说,主要存在三种位置的包:项目自身的包组织为本地(local)包;传统的存放在 $GOPATH 下的依赖包为外部(external)依赖包;被 govendor 管理的放在 vendor 目录下的依赖包则为 vendor 包。

安装与使用

安装

go get -u -v github.com/kardianos/govendor

把GOPATH加入到PATH中,可以直接使用,不用使用绝对路径了。

#进入到项目目录cd /Users/youdi/go/src/golang#初始化vendor目录govendor init#查看vendor目录ls
  rwxr-xr-x  youdi  staff   96 B    Sun Nov 18 22:52:53 2018    DemoRpc/
  rwxr-xr-x  youdi  staff  128 B    Fri Nov  2 09:32:52 2018    VideoServer/
  rwxr-xr-x  youdi  staff   96 B    Thu Oct 25 20:16:00 2018    cgo/
  rwxr-xr-x  youdi  staff  192 B    Mon Oct 29 22:39:08 2018    cloud/
  rwxr-xr-x  youdi  staff   96 B    Sun Nov 18 23:54:27 2018    coroutinue/
  rwxr-xr-x  youdi  staff  160 B    Mon Nov  5 15:25:12 2018    crawler/
  rwxr-xr-x  youdi  staff  128 B    Fri Oct 26 22:52:49 2018    errror/
  rwxr-xr-x  youdi  staff    1 KiB  Mon Nov  5 17:24:14 2018    example/
  rwxr-xr-x  youdi  staff  128 B    Thu Nov  1 23:08:53 2018    goWeb/
  rwxr-xr-x  youdi  staff  128 B    Thu Nov  1 16:45:35 2018    gopl/
  rwxr-xr-x  youdi  staff  224 B    Thu Oct 25 21:09:52 2018    gopl-zh/
  rwxr-xr-x  youdi  staff  256 B    Sat Oct 27 17:03:15 2018    goroutine/
  rwxr-xr-x  youdi  staff  128 B    Sun Nov 18 22:56:50 2018    gorpc/
  rwxr-xr-x  youdi  staff  128 B    Sat Oct 27 17:34:55 2018    http/
  rwxr-xr-x  youdi  staff  128 B    Sun Oct 28 09:59:14 2018    maze/
  rwxr-xr-x  youdi  staff  384 B    Thu Oct 25 21:09:29 2018    quickstart/
  rwxr-xr-x  youdi  staff   96 B    Sun Oct 28 10:54:10 2018    regex/
  rwxr-xr-x  youdi  staff  160 B    Mon Oct 29 16:14:30 2018    rpc/
  rwxr-xr-x  youdi  staff  576 B    Fri Oct 26 18:57:11 2018    rpcDemo/
  rwxr-xr-x  youdi  staff  320 B    Mon Oct 29 14:32:47 2018    spider/
  rwxr-xr-x  youdi  staff  128 B    Mon Nov 12 11:27:11 2018    tenx/
  rwxr-xr-x  youdi  staff  256 B    Sun Nov 18 22:10:32 2018    test/
  rwxr-xr-x  youdi  staff  256 B    Sun Dec  2 20:30:39 2018    vendor/
  rwxr-xr-x  youdi  staff  256 B    Fri Oct 26 17:26:54 2018    web/#将GOPATH中本工程使用到的依赖包自动移动到vendor目录中#说明:如果本地GOPATH没有依赖包,先go get相应的依赖包govendor add +external
或使用缩写: govendor add +e 

#Go 1.6以上版本默认开启 GO15VENDOREXPERIMENT 环境变量,可忽略该步骤。#通过设置环境变量 GO15VENDOREXPERIMENT=1 使用vendor文件夹构建文件。#可以选择 export GO15VENDOREXPERIMENT=1 或 GO15VENDOREXPERIMENT=1 go build 执行编译export GO15VENDOREXPERIMENT=1

govendor只是用来管理项目的依赖包,如果GOPATH中本身没有项目的依赖包,则需要通过go get先下载到GOPATH中,再通过govendor add +external拷贝到vendor目录中。Go 1.6以上版本默认开启GO15VENDOREXPERIMENT环境变量。

govendor使用命令

govendor (v1.0.9): record dependencies and copy into vendor folder
    -govendor-licenses    Show govendor's licenses.
    -version              Show govendor version
    -cpuprofile 'file'    Writes a CPU profile to 'file' for debugging.
    -memprofile 'file'    Writes a heap profile to 'file' for debugging.

Sub-Commands

    init     Create the "vendor" folder and the "vendor.json" file.
    list     List and filter existing dependencies and packages.
    add      Add packages from $GOPATH.
    update   Update packages from $GOPATH.
    remove   Remove packages from the vendor folder.
    status   Lists any packages missing, out-of-date, or modified locally.
    fetch    Add new or update vendor folder packages from remote repository.
    sync     Pull packages into vendor folder from remote repository with revisions
                 from vendor.json file.
    migrate  Move packages from a legacy tool to the vendor folder with metadata.
    get      Like "go get" but copies dependencies into a "vendor" folder.
    license  List discovered licenses for the given status or import paths.
    shell    Run a "shell" to make multiple sub-commands more efficient for large
                 projects.

    go tool commands that are wrapped:
      "+status" package selection may be used with them
    fmt, build, install, clean, test, vet, generate, tool

Status Types

    +local    (l) packages in your project
    +external (e) referenced packages in GOPATH but not in current project
    +vendor   (v) packages in the vendor folder
    +std      (s) packages in the standard library

    +excluded (x) external packages explicitly excluded from vendoring
    +unused   (u) packages in the vendor folder, but unused
    +missing  (m) referenced packages but not found

    +program  (p) package is a main package

    +outside  +external +missing
    +all      +all packages

    Status can be referenced by their initial letters.

Package specifier
    <path>[::<origin>][{/...|/^}][@[<version-spec>]]

Ignoring files with build tags, or excluding packages from being vendored:
    The "vendor.json" file contains a string field named "ignore".
    It may contain a space separated list of build tags to ignore when
    listing and copying files.
    This list may also contain package prefixes (containing a "/", possibly
    as last character) to exclude when copying files in the vendor folder.
    If "foo/" appears in this field, then package "foo" and all its sub-packages
    ("foo/bar", …) will be excluded (but package "bar/foo" will not).
    By default the init command adds the "test" tag to the ignore list.

If using go1.5, ensure GO15VENDOREXPERIMENT=1 is set.

具体来看,这些包可能的类型如下:

状态缩写含义
+locall本地包,即项目自身的包组织
+externale外部包,即被 $GOPATH 管理,但不在 vendor 目录下
+vendorv已被 govendor 管理,即在 vendor 目录下
+stds标准库中的包
+unusedu未使用的包,即包在 vendor 目录下,但项目并没有用到
+missingm代码引用了依赖包,但该包并没有找到
+programp主程序包,意味着可以编译为执行文件
+outside
外部包和缺失的包
+all
所有的包

常见的命令如下,格式为 govendor COMMAND。

通过指定包类型,可以过滤仅对指定包进行操作。

命令功能
init初始化 vendor 目录
list列出所有的依赖包
add添加包到 vendor 目录,如 govendor add +external 添加所有外部包
add PKG_PATH添加指定的依赖包到 vendor 目录
update从 $GOPATH 更新依赖包到 vendor 目录
remove从 vendor 管理中删除依赖
status列出所有缺失、过期和修改过的包
fetch添加或更新包到本地 vendor 目录
sync本地存在 vendor.json 时候拉去依赖包,匹配所记录的版本
get类似 go get 目录,拉取依赖包到 vendor 目录

上面是命令,我们看一下vendor目录和配置

  ll vendor/
  
  rwxr-xr-x  youdi  staff  832 B    Thu Nov 29 23:56:45 2018    github.com/
  rwxr-xr-x  youdi  staff   96 B    Thu Nov 29 23:56:45 2018    golang.org/
  rwxr-xr-x  youdi  staff  128 B    Thu Nov 29 23:56:45 2018    google.golang.org/
  rwxr-xr-x  youdi  staff  192 B    Thu Nov 29 23:56:45 2018    gopkg.in/
  rwxr-xr-x  youdi  staff  128 B    Thu Nov 29 23:56:45 2018    quickstart/
  rw-r--r--  youdi  staff   27 KiB  Sun Dec  2 20:30:39 2018    vendor.json

配置文件:

cat vendor.json

{    "comment": "",    "ignore": "test",    "package": [
        {            "checksumSHA1": "l1dgFJfzGLG/2AEPyt6ADsnVLPQ=",            "path": "github.com/PuerkitoBio/goquery",            "revision": "dc2ec5c7ca4d9aae063b79b9f581dd3ea6afd2b2",            "revisionTime": "2018-06-07T15:06:10Z"
        },
        {            "checksumSHA1": "He9ESHVFPcUu6Q8wB6UWHWx+yTo=",            "path": "github.com/anaskhan96/soup",            "revision": "00be3d730c89ee1efa038324e9bc7355ad23f93b",            "revisionTime": "2018-06-22T12:59:56Z"
        },
        {            "checksumSHA1": "Q/2QpI7E35SsNYfaxLsWHFry9k4=",            "path": "github.com/andybalholm/cascadia",            "revision": "901648c87902174f774fac311d7f176f8647bdaa",            "revisionTime": "2018-02-20T18:43:36Z"
        }, ....



作者:若与
链接:https://www.jianshu.com/p/8bc30c2428d4


打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP