设为首页 加入收藏

TOP

golang开发:(四)包管理器 glide的使用
2019-06-02 14:07:59 】 浏览:105
Tags:golang 开发 管理 glide 使用

glide 是golang项目开发中是特别重要的软件,没有它,golang的项目可能都无法发布。

为什么要使用glide

平时我们开发Go项目的时候,使用第三方的包的时候都直接使用go get 去获取第三方的包,但是go get获取到的包是项目的develop分支,我们开发的时候倒是可以不怎么关注。但是如果到了生产环境,直接使用go get 是有很大风险的,因为,众所周知,develop是开发分支,维护者会把新的代码push到开发分支,如果我们使用go get的话,可能我们每次发布版本获取到的第三方代码都是不一致,这样项目就会有特别大的风险。我们肯定希望go get 第三方包到我们项目中的时候,可以设置一个稳定的版本来使用。但是go get却无法满足这个最普遍的要求。然后,glide就横空出世了。

怎么使用glide

安装glide

mac系统或者Linux系统安装
curl https://glide.sh/get | sh

Mac也可brew安装
brew install glide

Ubuntu也可以apt-get安装
sudo add-apt-repository ppa:masterminds/glide && sudo apt-get update
sudo apt-get install glide

完整之后测试下是否安装成功
glide -h

NAME:
   glide - Vendor Package Management for your Go projects.

   Each project should have a 'glide.yaml' file in the project directory. Files
   look something like this:

       package: github.com/Masterminds/glide
       imports:
       - package: github.com/Masterminds/cookoo
         version: 1.1.0
       - package: github.com/kylelemons/go-gypsy
         subpackages:
         - yaml

   For more details on the 'glide.yaml' files see the documentation at
   https://glide.sh/docs/glide.yaml


USAGE:
   glide [global options] command [command options] [arguments...]

VERSION:
   v0.13.2

COMMANDS:
     create, init       Initialize a new project, creating a glide.yaml file
     config-wizard, cw  Wizard that makes optional suggestions to improve config in a glide.yaml file.
     get                Install one or more packages into `vendor/` and add dependency to glide.yaml.

出现上面的提示信息界面就表示安装成功了。
介绍几个平时开发用的比较多的几个命令,掌握了这几个命令项目开发就基本没啥问题了。

glide init --初始化项目,生成glide.yaml
glide install --安装第三方包
glide up --更新第三方包

举个栗子

做个UUID使用案例
首先 go get github.com/satori/go.uuid

package main

import (
    "fmt"
    uuid2 "github.com/satori/go.uuid"
)

func main() {
    uuid,_ := uuid2.NewV4()

    fmt.Println(uuid)
}

运行下

10c2b95f-b7c2-45f3-b5a3-a69020b9a7f7
Process finished with exit code 0

然后进入到项目目录

glide init
会生成一个包含UUID包的yaml 文件
package: test
import:
- package: github.com/satori/go.uuid

我们给这个包加下版本号

package: test
import:
- package: github.com/satori/go.uuid
- version: 1.2.0
然后执行 
glide install
显示里面有设置版本号的信息
[INFO]  --> Fetching updates for github.com/satori/go.uuid
[INFO]  --> Setting version for github.com/satori/go.uuid to v1.2.0.
我们看到在项目包里面生成一个 vendor的文件夹,vendor里面有个uuid 的包
vendor/github.com/satori/go.uuid,以后通过glide管理的包文件就在vendor里面。
如果我们想把 version: 1.2.0 该为 version: 1.1.0.修改yaml文件的版本号,然后执行
glide up
[INFO]  --> Fetching updates for github.com/satori/go.uuid
[INFO]  --> Setting version for github.com/satori/go.uuid to v1.1.0.
vendor里面的版本就切换到了v1.1.0

glide 特别好用,特别实用吧。

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇golang中,map作为函数参数是如何.. 下一篇[开源]Gin + GORM + Casbin+vue-e..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目