Golang包管理工具之govendor的使用

govendor简介

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

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

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

安装与使用

安装

C:\Users\Administrator>go get -u -v github.com/kardianos/govendor
github.com/kardianos/govendor (download)
github.com/kardianos/govendor/pkgspec
github.com/kardianos/govendor/prompt
github.com/kardianos/govendor/internal/pathos
github.com/kardianos/govendor/vendor/github.com/Bowery/prompt
github.com/kardianos/govendor/internal/vos
github.com/kardianos/govendor/vendorfile
github.com/kardianos/govendor/vendor/github.com/dchest/safefile
github.com/kardianos/govendor/vendor/github.com/pkg/errors
github.com/kardianos/govendor/vendor/gopkg.in/yaml.v2
github.com/kardianos/govendor/internal/vfilepath
github.com/kardianos/govendor/vendor/golang.org/x/tools/go/vcs
github.com/kardianos/govendor/vcs
github.com/kardianos/govendor/vendor/github.com/google/shlex
github.com/kardianos/govendor/cliprompt
github.com/kardianos/govendor/context
github.com/kardianos/govendor/migrate
github.com/kardianos/govendor/help
github.com/kardianos/govendor/run

验证

C:\Users\Administrator>govendor -version
v1.0.9

使用

#进入到项目目录
cd /home/gopath/src/project
#初始化vendor目录
govendor init
#将GOPATH中本工程使用到的依赖包自动移动到vendor目录中
#说明:如果本地GOPATH没有依赖包,先go get相应的依赖包
govendor add +external
或使用缩写: govendor add +e 

常用命令

C:\Users\Administrator>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
        [::][{/...|/^}][@[]]

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.

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

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

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

子命令:

状态 缩写状态 含义
+local l 本地包,即项目自身的包组织
+external e 外部包,即被 $GOPATH 管理,但不在 vendor 目录下
+vendor v 已被 govendor 管理,即在 vendor 目录下
+std s 标准库中的包
+unused u 未使用的包,即包在 vendor 目录下,但项目并没有用到
+missing m 代码引用了依赖包,但该包并没有找到
+program p 主程序包,意味着可以编译为执行文件
+outside 外部包和缺失的包
+all 所有的包

你可能感兴趣的:(go学习)