您的位置:首页 > 编程语言 > Go语言

Golang-and-package-version-managment

2016-01-20 00:06 549 查看
参考文章

学习
Golang
之后对golang中的版本管理,包管理等机制一直没有很好的琢磨,偶然想起还是觉得很有必要进行归纳,包管理使用起来简单,无非就是
install
uninstall
list
等,但是在工程上包管理要是不当,却会造成很多不必要的麻烦。

gvm

gvm是golang的版本管理程序,为什么需要这么个版本管理,是因为golang还在高速发展中,发版频繁,升级迭代可能不完全兼容上一代,所以这种工具应运而生,像ruby有对应的
rvm
,node有对应的
nvm
都是为了方便的进行不同版本的切换。

install

bash < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)


安装完,验证一下是否安装成功

gvm version


输出

Go Version Manager v1.0.22 installed at /Users/liujb/.gvm


支持的命令

% gvm
Usage: gvm [command]

Description:
GVM is the Go Version Manager

Commands:
version    - print the gvm version number
get        - gets the latest code (for debugging)
use        - select a go version to use
diff       - view changes to Go root
implode    - completely remove gvm
install    - install go versions
uninstall  - uninstall go versions
cross      - install go cross compilers
linkthis   - link this directory into GOPATH
list       - list installed go versions
listall    - list available versions
alias      - manage go version aliases
pkgset     - manage go packages sets
pkgenv     - edit the environment for a package set


常见的命令

gvm listall
查看所有的golang版本

gvm install go1.4.2
安装1.4.2版本

gvm use go1.4.2
使用go1.4.2版本,使用后
GOPATH
变成
/Users/liujb/.gvm/pkgsets/go1.4.2/global
,这样就相当于每个版本的golang所使用的GOPATH都不一样,这样不同的golang包之间的版本兼容问题也可以得到解决。

gvp/gpm

install

brew install gvp
brew install gpm


use

这两个包一般成对使用,其中
gvp
命令使用主要设置
GOPATH
为当前目录,例如

source gvp
echo $GOPATH    # 输出/Users/liujb/.godeps:/Users/liujb


gpm
主要是通过
install
命令安装
Godeps
文件内指定的包,最主要的两个命令

$ gpm get         # Parses the Godeps file, gets dependencies and sets them
# to the appropriate version but does not install them.
$ gpm install     # Parses the Godeps file, installs dependencies and sets


Godeps
的文件类似

github.com/nu7hatch/gotrail               v0.0.2
github.com/replicon/fast-archiver         v1.02
launchpad.net/gocheck                     r2013.03.03   # Bazaar repositories are supported
code.google.com/p/go.example/hello/...    ae081cd1d6cc  # And so are Mercurial ones


通过
gpm install
时候会先解析
Godeps
文件,然后安装文件内指定的包以及版本,同时安装到
.godeps
目录下的
src/bin/pkg


godep

install

go get github.com/tools/godep
或者
brew install godep


使用

参考1 参考2

在项目的根目录下,执行两个命令,其中需要安装的包需要先使用
go get
下载到
$GOPATH
下边,另外所在项目需要在版本管理
git
hg
之下。

godep save
godep restore


Godeps.json的文件格式如下

{
"ImportPath": "github.com/kr/hk",
"GoVersion": "go1.1.2",
"Deps": [
{
"ImportPath": "code.google.com/p/go-netrc/netrc",
"Rev": "28676070ab99"
},
{
"ImportPath": "github.com/kr/binarydist",
"Rev": "3380ade90f8b0dfa3e363fd7d7e941fa857d0d13"
}
]
}


gom

install

go get github.com/mattn/gom


use

创建Gomfiles

gom 'github.com/mattn/go-runewidth', :tag => 'go1'
gom 'github.com/mattn/go-scan', :commit => 'ecb144fb1f2848a24ebfdadf8e64380406d87206'
gom 'github.com/daviddengcn/go-colortext'
gom 'github.com/mattn/go-ole', :goos => 'windows'

# Execute only in the "test" environment.
group :test do
gom 'github.com/mattn/go-sqlite3'
end

# Execute only for the "custom_group" group.
group :custom_group do
gom 'github.com/golang/lint/golint'
end


执行

gom install


然后会在项目下创建
_vendor
目录。

% tree -L 4
├── Gomfile
└── _vendor
└── src
└── github.com
├── daviddengcn
└── mattn

5 directories, 1 file


个人小结

gvm
说白了是
golang
的版本管理,跟包管理没有半毛钱关系

gvp/gpm
这一配套工具,可以设置当前目录为
GOPATH
,同时根据
Godeps
文件的描述,安装指定的包到gvp指定的路径。

godep
说白了,就是将通过
go get
安装的包,通过json文件
Godeps.json
保存到项目下的
Godeps/_workspace
目录下,然后编译的时候
export GOPATH='Project/Godeps/_workspace':$GOPATH
即可。

gom
npm
最接近,都只是需要一个包描述文件,然后安装到指定位置,其余什么都不用做了,当然别忘了指定
GOPATH



对golang中包的管理机制大概讲这么多,有问题一起沟通。引用注明出处即可。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: