dep使用指南

2018-07-14  本文已影响302人  三无架构师

[TOC]

Dep

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

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

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

安装

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

使用

使用流程:

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命令,其会自动下载依赖包,其会自动做以下事情


如果需手动添加一个具体的依赖,可以使用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后,其下面会出现以下文件或者目录

其关系如下:

image.png

文件语法

Gopkg.toml语法

1.注释: 使用#

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

其格式如下:


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"

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/时被废弃,其可以为全局,也可以针对特定项目,其目前可用操作:

格式如:


[prune]

  non-go = true

  [[prune.project]]

    name = "github.com/project/name"

    go-tests = true

    non-go = false

Gopkg.lock

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

上一篇下一篇

猜你喜欢

热点阅读