当前位置: 移动技术网 > IT编程>脚本编程>Go语言 > golang开发:(四)包管理器 glide的使用

golang开发:(四)包管理器 glide的使用

2019年06月03日  | 移动技术网IT编程  | 我要评论

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 特别好用,特别实用吧。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网