美文网首页
dep使用指南

dep使用指南

作者: 三无架构师 | 来源:发表于2018-07-14 15:33 被阅读302次

    [TOC]

    Dep

    vendor特性只用来存储本地依赖

    dep为官方支持的实验性的工具,其官网地址.目前start已经9673

    其通过两个metadata文件来管理依赖:

    • Gopkg.toml:定义用户相关内容,如依赖的sourcebranchversion等.可以通过命令生产,也可以被用户根据 需要手动修改

    • Gopkg.lock: 定义具体状态,例如各依赖的revision。自动生成的,不可以修改

    安装

    直接从官网下载相应平台的二进制包,放入到环境变量中即可(为了使用更方便,可以进行重命名一下)

    使用

    使用流程:

    1. 创建项目

    2. 执行dep init进行初始化

    3. 写代码并添加引用

    其基本语法如下:

    
    PS C:\Users\liukun> dep
    
    Dep is a tool for managing dependencies for Go projects
    
    Usage: "dep [command]"
    
    Commands:
    
      init Set up a new Go project, or migrate an existing one
    
      status Report the status of the project's dependencies
    
      ensure Ensure a dependency is safely vendored in the project
    
      prune Pruning is now performed automatically by dep ensure.
    
      version Show the dep version information
    
    Examples:
    
      dep init set up a new project
    
      dep ensure install the project's dependencies
    
      dep ensure -update update the locked versions of all dependencies
    
      dep ensure -add github.com/pkg/errors add a dependency to the project
    
    Use "dep help [command]" for more information about a command.
    
    

    初始化使用:dep init命令,其会自动下载依赖包,其会自动做以下事情

    • 分析当前代码包中的依赖关系

    • 将分析出的直接依赖/约束写到Gopkg.toml

    • 将项目依赖的第三方包,在满足Gopkg.toml中约束范围内的最新version/branch/revision信息写入Gopkg.lock文件中

    • 创建vendor目录,并以Gopkg.lock为输入,将其中包下载到vendor下面


    如果需手动添加一个具体的依赖,可以使用dep ensure -add xxx

    建议直接使用dep ensure,其会自动在vendor目录中增加或更新相关依赖(add/update/remove)


    查看状态:dep status


    其缓存是放在$GOPATH/pkg/dep/sources里面

    Dep通过两个metadata文件来管理依赖: manifest文件Gopkg.toml和lock文件Gopkg.lock

    $GOPATH/src下创建项目目录,如foo,执行dep init后,其下面会出现以下文件或者目录

    • vendor:目录

    • Gopkg.lock:描述依赖的具体状态,例如各依赖的revision

    • Gopkg.toml:描述用户的意图,包括依赖的 source、branch、version等

    其关系如下:

    image.png

    文件语法

    Gopkg.toml语法

    • Dependency rules: constraintsoverrides用于指定依赖的版本以及检索地址

    • Package graph rules: requiredignored用于包含或排除其相应的包

    • metadata:用户定义的键值对map,其会被dep忽略

    • prune: 用于指定哪些文件或目录不是必需的,其不会被放入到vendor

    1.注释: 使用#

    2.required:是一个包(不是projects)的列表,该包具有以下特性;

    • 被项目用到

    • 没有被直接或者传递import

    • 不想被加入到GOPAT中,也不想lock它的版本

    其格式如下:

    
    required = ["github.com/user/thing/cmd/thing"]
    
    

    3.ignored:是一个包(不是projects)的列表,避免某些package加入到依赖中,格式如下:

    
    ignored = ["github.com/user/project/badpkg"]
    
    

    4.constraint: 指定直接依赖的相关信息。其格式如下

    
    [[constraint]]
    
      # Required: the root import path of the project being constrained.
    
      name = "github.com/user/project"
    
      # Recommended: the version constraint to enforce for the project.
    
      # Note that only one of "branch", "version" or "revision" can be specified.
    
      version = "1.0.0"
    
      branch = "master"
    
      revision = "abc123"
    
      # Optional: an alternate location (URL or import path) for the project's source.
    
      source = "https://github.com/myfork/package.git"
    
      # Optional: metadata about the constraint or override that could be used by other independent systems
    
      [metadata]
    
      key1 = "value that convey data to other systems"
    
      system1-data = "value that is used by a system"
    
      system2-data = "value that is used by another system"
    
    
    • name: 指定依赖的项目地址

    • version|branch|revision: 指定依赖的版本,这三者只能同时存在一个

      • version:其对应为tag。是constraintversion中的属性,用来指定依赖的版本。其支持版本比较操作

        • =: equal

        • !=: not equal

        • >: greater than

        • <: less than

        • >=: greater than or equal to

        • <=: less than or equal to

        • -: literal range. E.g., 1.2 - 1.4.5 is equivalent to >= 1.2, <= 1.4.5

        • ~: minor range. E.g., ~1.2.3 is equivalent to >= 1.2.3, < 1.3.0

        • ^: major range. E.g., ^1.2.3 is equivalent to >= 1.2.3, < 2.0.0

        • [xX*]: wildcard. E.g., 1.2.x is equivalent to >= 1.2.0, < 1.3.0

      • branch: 其对应为分支

      • revision: 其为commit id

    5.override:跟constraint数据结构相同,用来指定传递依赖的相关信息。

    6.metadata: 项目元数据,为基本描述信息,其可以存在于root下,也可以存在于contraintoverride下,如:

    
    [metadata]
    
    key1 = "value that convey data to other systems"
    
    system1-data = "value that is used by a system"
    
    

    7. prune:在其下定义的文件在写vendor/时被废弃,其可以为全局,也可以针对特定项目,其目前可用操作:

    • unused-packages indicates that files from directories that do not appear in the package import graph should be pruned.

    • non-go prunes files that are not used by Go.

    • go-tests prunes Go test files.

    格式如:

    
    [prune]
    
      non-go = true
    
      [[prune.project]]
    
        name = "github.com/project/name"
    
        go-tests = true
    
        non-go = false
    
    

    Gopkg.lock

    在使用dep initdep ensure时会自动生成

    相关文章

      网友评论

          本文标题:dep使用指南

          本文链接:https://www.haomeiwen.com/subject/zudypftx.html