Go语言包管理工具dep

什么是dep?git

dep和go,在必定程度上至关于maven之于Java,composer之于PHP,dep是go语言官方的一个包管理工具。github

相比较go get而言,dep能够直接给引入的第三方包一个专门的目录,而且能够专门制定一个配置文件,控制go项目所引入的包,版本以及其余依赖关系。golang

dep这个项目放在golang官方的github中:https://github.com/golang/depwindows

官方对于dep的解释是:dep is the official experiment, but not yet the official tool. 也就是说,dep目前还处于试验阶段,还并无成为一个官方意义上的工具。毕竟go语言还很年轻,可是这也充分的证实了go语言的生态圈十分丰富。缓存

安装composer

安装dep工具的方式有不少种,若是是mac电脑的话,只须要以下命令:框架

brew install dep

对于Linux和类Unix系统而言,咱们还可使用以下方式安装dep:curl

curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh

或者直接使用源码安装。maven

而对于windows电脑,可能会相对来讲麻烦些,咱们能够直接使用源码编译安装或者直接使用go get命令安装:函数

go get -u github.com/golang/dep/cmd/dep

待安装完成以后,将dep.exe放在环境变量就可使用了。

使用

接下来咱们来看一下dep的使用方式。

当安装好dep以后,咱们在命令行中,输入dep就能够看到有关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
  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通常进场会使用3个命令:

init-用来初始化项目

status-用来查看当前项目的依赖包的状态

ensure-用来同步包的配置文件和引入的包

下面咱们正式使用dep来建立一个项目。首先创建一个项目路径,这里咱们将项目路径叫作depProject。而后在项目路径中创建src源代码目录。在src中创建一个存放dep文件和项目主文件的目录,咱们暂且能够叫作depmain,并创建一个go文件。

这样咱们的目录结构以下:

depProject
    |----src
          |----depmain
                  |-----main.go

创建好以后,咱们在main.go中写一个简单的go程序:

1 package main
2 
3 import (
4     "fmt"
5 )
6 func main() {
7     fmt.Println("hello)
8 }

以后咱们在这个目录下运行以下命令:

dep init

运行完成以后,dep就会为咱们自动生成以下文件和目录:

有点像常规go项目的样子了,不过须要注意的是pkg中存放的go语言引入包的缓存文件,vendor中存放的是真正的引入的包内容。接下来是两个文件,Gopkg.lock和Gopkg.toml。Gopkg.lock文件是自动生成的,而Gopkg.toml文件是咱们能够编辑的文件,经过编辑这个文件,并运行dep的命令能够达到引入包的目的:

# 必需包
required = ["github.com/gin-gonic/gin"]
# 忽略包
#ignored = []没有能够不写
# 项目元数据
#[metadata]


# 约束条件
[[constraint]]
  # name = 
  # 可选:版本
  # version =
  # 分支
  # branch
  # 修订
  # revision
  # 可选:指定来源
  # source = "github.com/gin-gonic/gin"

以上代码是一个示例,咱们写好以后运行

dep ensure

就能够了,咱们会看到vendor下多了一些有关此包的依赖和引入。

咱们引入了gin框架的包,因此咱们如今就可使用gin框架了,写的时候,和咱们平时的go语言项目同样:

 1 package main
 2 
 3 import "github.com/gin-gonic/gin"
 4 
 5 func main() {
 6     r := gin.Default()
 7     r.GET("/ping", func(c *gin.Context) {
 8         c.JSON(200, gin.H{
 9             "message": "pong",
10         })
11     })
12     r.Run() // listen and serve on 0.0.0.0:8080
13 }

这样作彻底没有问题,咱们只用考虑这个包本来的路径github.com/gin-gonic/gin就好。

接下来咱们就能够编译运行这个项目了。

另外须要注意的是,使用dep管理包控制依赖的时候,若是咱们须要新建目录,并编写本身的新的包名的时候,只须要在src下新建目录就能够了。这样作才能正确引入。

好比:咱们要编写一个add的函数,咱们能够这样,在src下简历一个utils目录,下写个add.go文件:

1 package utils
2 
3 func Add(a int, b int) int {
4     return a+b
5 }

这样在主程序中,这样写就能够import本身写的包,并使用本身的函数了:

package main

import (
    "utils"
    "fmt"
)
func main() {
    fmt.Println("hello")
    utils.Add(1, 1)
}
相关文章
相关标签/搜索