這是一個建立於
的文章,其中的資訊可能已經有所發展或是發生改變。
參考文章
學習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
支援的命令
% gvmUsage: gvm [command]Description: GVM is the Go Version ManagerCommands: 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 gvpbrew install gpm
use
這兩個包一般成對使用,其中gvp命令使用主要設定GOPATH為目前的目錄,例如
source gvpecho $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.2github.com/replicon/fast-archiver v1.02launchpad.net/gocheck r2013.03.03 # Bazaar repositories are supportedcode.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 savegodep 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 └── mattn5 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中包的管理機制大概講這麼多,有問題一起溝通。引用註明出處即可。