1.1 Go 安裝
創(chuàng)新互聯(lián)建站服務(wù)項(xiàng)目包括花山網(wǎng)站建設(shè)、花山網(wǎng)站制作、花山網(wǎng)頁制作以及花山網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,花山網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到花山省份的部分城市,未來相信會繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
Go的三種安裝方式
Go有多種安裝方式,你可以選擇自己喜歡的。這里我們介紹三種最常見的安裝方式:
Go源碼安裝:這是一種標(biāo)準(zhǔn)的軟件安裝方式。對于經(jīng)常使用Unix類系統(tǒng)的用戶,尤其對于開發(fā)者來說,從源碼安裝可以自己定制。
Go標(biāo)準(zhǔn)包安裝:Go提供了方便的安裝包,支持Windows、Linux、Mac等系統(tǒng)。這種方式適合快速安裝,可根據(jù)自己的系統(tǒng)位數(shù)下載好相應(yīng)的安裝包,一路next就可以輕松安裝了。**推薦這種方式**
第三方工具安裝:目前有很多方便的第三方軟件包工具,例如Ubuntu的apt-get、Mac的homebrew等。這種安裝方式適合那些熟悉相應(yīng)系統(tǒng)的用戶。
最后,如果你想在同一個(gè)系統(tǒng)中安裝多個(gè)版本的Go,你可以參考第三方工具GVM,這是目前在這方面做得最好的工具,除非你知道怎么處理。
Go源碼安裝
在Go的源代碼中,有些部分是用Plan 9 C和ATT匯編寫的,因此假如你要想從源碼安裝,就必須安裝C的編譯工具。
在Mac系統(tǒng)中,只要你安裝了Xcode,就已經(jīng)包含了相應(yīng)的編譯工具。
在類Unix系統(tǒng)中,需要安裝gcc等工具。例如Ubuntu系統(tǒng)可通過在終端中執(zhí)行sudo apt-get install gcc
libc6-dev來安裝編譯工具。
在Windows系統(tǒng)中,你需要安裝MinGW,然后通過MinGW安裝gcc,并設(shè)置相應(yīng)的環(huán)境變量。
你可以直接去官網(wǎng)下載源碼,找相應(yīng)的goVERSION.src.tar.gz的文件下載,下載之后解壓縮到$HOME目錄,執(zhí)行如下代碼:
cd go/src
./all.bash
運(yùn)行all.bash后出現(xiàn)"ALL TESTS PASSED"字樣時(shí)才算安裝成功。
上面是Unix風(fēng)格的命令,Windows下的安裝方式類似,只不過是運(yùn)行all.bat,調(diào)用的編譯器是MinGW的gcc。
如果是Mac或者Unix用戶需要設(shè)置幾個(gè)環(huán)境變量,如果想重啟之后也能生效的話把下面的命令寫到.bashrc或者.zshrc里面,
export GOPATH=$HOME/gopath
export PATH=$PATH:$HOME/go/bin:$GOPATH/bin
如果你是寫入文件的,記得執(zhí)行bash .bashrc或者bash
.zshrc使得設(shè)置立馬生效。
如果是window系統(tǒng),就需要設(shè)置環(huán)境變量,在path里面增加相應(yīng)的go所在的目錄,設(shè)置gopath變量。
當(dāng)你設(shè)置完畢之后在命令行里面輸入go,看到如下圖片即說明你已經(jīng)安裝成功
圖1.1 源碼安裝之后執(zhí)行Go命令的圖
如果出現(xiàn)Go的Usage信息,那么說明Go已經(jīng)安裝成功了;如果出現(xiàn)該命令不存在,那么可以檢查一下自己的PATH環(huán)境變中是否包含了Go的安裝目錄。
關(guān)于上面的GOPATH將在下面小節(jié)詳細(xì)講解
Go標(biāo)準(zhǔn)包安裝
Go提供了每個(gè)平臺打好包的一鍵安裝,這些包默認(rèn)會安裝到如下目錄:/usr/local/go
(Windows系統(tǒng):c:\Go),當(dāng)然你可以改變他們的安裝位置,但是改變之后你必須在你的環(huán)境變量中設(shè)置如下信息:
export GOROOT=$HOME/go
export GOPATH=$HOME/gopath
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
上面這些命令對于Mac和Unix用戶來說最好是寫入.bashrc或者.zshrc文件,對于windows用戶來說當(dāng)然是寫入環(huán)境變量。
下載go的zip文件。并且一定要把文件解壓到c:\go目錄下。2、配置windows的高級環(huán)境變量。包括:GOROOT、GOOS、GOBIN、GOARCH。并且在path變量里面把c:\go\bin加入。以便可以在命令行直接運(yùn)行g(shù)o命令。
1、編譯vimgdb
下載vimgdb73和vim73
mkdir -p ./tmp
cd tmp
tar zxvf ../vim-7.3.tar.gz
unzip ../vimgdb-for-vim7.3-master.zip
mv vimgdb-for-vim7.3-master vimgdb-for-vim7.3
patch -p0 vimgdb-for-vim7.3/vim73.patch
cd vim73
安裝依賴
sudo apt-get install build-essential
sudo apt-get build-dep vim-gtk
sudo apt-get install libncurses5-dev
安裝
// 這里直接執(zhí)行make的操作
make
sudo make install
安裝vimgdb runtime
cd ../vimgdb-for-vim7.3
cp vimgdb_runtime ~/.vim/bundle
打開vim
:helptags ~/.vim/bundle/vimgdb_runtime/doc " 生成doc文件
添加配置.vimrc
" vimgdb插件
run macros/gdb_mappings.vim
在vim中執(zhí)行g(shù)db時(shí),報(bào) “Unable to read from GDB pseudo tty” 的錯(cuò)誤,因?yàn)闆]有安裝 gdb ,所以安裝gdb
sudo apt-get install gdb
2、安裝vundle
set up vundle
$ git clone ~/.vim/bundle/vundle
Configure Plugins
在.vimrc文件的開頭添加下面的內(nèi)容,有些不是必須的,可以注掉
set nocompatible " be iMproved, required
filetype off " required
" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/vundle/
call vundle#rc()
" alternatively, pass a path where Vundle should install plugins
"let path = '~/some/path/here'
"call vundle#rc(path)
" let Vundle manage Vundle, required
Plugin 'gmarik/vundle'
" The following are examples of different formats supported.
" Keep Plugin commands between here and filetype plugin indent on.
" scripts on GitHub repos
Plugin 'tpope/vim-fugitive'
Plugin 'Lokaltog/vim-easymotion'
Plugin 'tpope/vim-rails.git'
" The sparkup vim script is in a subdirectory of this repo called vim.
" Pass the path to set the runtimepath properly.
Plugin 'rstacruz/sparkup', {'rtp': 'vim/'}
" scripts from
Plugin 'L9'
Plugin 'FuzzyFinder'
" scripts not on GitHub
Plugin 'git://git.wincent.com/command-t.git'
" git repos on your local machine (i.e. when working on your own plugin)
Plugin ''
" ...
filetype plugin indent on " required
" To ignore plugin indent changes, instead use:
"filetype plugin on
"
" Brief help
" : PluginList - list configured plugins
" : PluginInstall(!) - install (update) plugins
" : PluginSearch(!) foo - search (or refresh cache first) for foo
" : PluginClean(!) - confirm (or auto-approve) removal of unused plugins
"
" see :h vundle for more details or wiki for FAQ
" NOTE: comments after Plugin commands are not allowed.
" Put your stuff after this line
Install Plugins
Launch vim and run
: PluginInstall
vim +PluginInstall +qall
3、官方vim-lang插件
Config vim file .vimrc,Add content bellow in bottom of the file
" 官方的插件
" Some Linux distributions set filetype in /etc/vimrc.
" Clear filetype flags before changing runtimepath to force Vim to
" reload them.
filetype off
filetype plugin indent off
set runtimepath+=$GOROOT/misc/vim
filetype plugin indent on
syntax on
autocmd FileType go autocmd BufWritePre Fmt
4、代碼補(bǔ)全的插件gocode
配置go的環(huán)境變量,比如我的配置,GOPATH變量是必須要配置的,PATH中必須把GOPATH的bin也添加進(jìn)去,否則沒有自動(dòng)提示,會提示找不到模式
export GOROOT=/usr/local/go
export GOPATH=/data/app/gopath
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
Set up gocode
Then you need to get the appropriate version of the gocode, for 6g/8g/5g compiler you can do this:
go get -u github.com/nsf/gocode (-u flag for "update")
Configure vim in .vimrc file
Plugin 'nsf/gocode', {'rtp': 'vim/'}
Install Plugins
Launch vim and run
: PluginInstall
vim +PluginInstall +qall
寫一個(gè)helloword程序,輸入fmt后按C-xC-o如果能看到函數(shù)的聲明展示出來,說明安裝是正確的。
4、代碼跳轉(zhuǎn)提示godef
Set up godef
go get -v code.google.com/p/rog-go/exp/cmd/godef
go install -v code.google.com/p/rog-go/exp/cmd/godef
git clone ~/.vim/bundle/vim-godef
Configure vim in .vimrc file
Bundle 'dgryski/vim-godef'
Install Plugins
Launch vim and run
: PluginInstall
vim +PluginInstall +qall
5、代碼結(jié)構(gòu)提示gotags
Set up gotags
go get -u github.com/jstemmer/gotags
Put the following configuration in your vimrc:
Bundle 'majutsushi/tagbar'
nmap :TagbarToggle
let g:tagbar_type_go = {
\ 'ctagstype' : 'go',
\ 'kinds' : [
\ 'p:package',
\ 'i:imports:1',
\ 'c:constants',
\ 'v:variables',
\ 't:types',
\ 'n:interfaces',
\ 'w:fields',
\ 'e:embedded',
\ 'm:methods',
\ 'r:constructor',
\ 'f:functions'
\ ],
\ 'sro' : '.',
\ 'kind2scope' : {
\ 't' : 'ctype',
\ 'n' : 'ntype'
\ },
\ 'scope2kind' : {
\ 'ctype' : 't',
\ 'ntype' : 'n'
\ },
\ 'ctagsbin' : 'gotags',
\ 'ctagsargs' : '-sort -silent'
\ }
命令模式下按在右邊就會顯示當(dāng)前文件下的函數(shù)名,結(jié)構(gòu)體名等等,光標(biāo)放到相應(yīng)的tag上,按回車可以快速跳到程序中的相應(yīng)位置。
再次按會關(guān)閉tag窗口。
PS:本地的.vimrc的配置
" 插件管理器 vundle
set nocompatible " be iMproved, required
filetype off " required
" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/vundle/
call vundle#rc()
" alternatively, pass a path where Vundle should install plugins
"let path = '~/some/path/here'
"call vundle#rc(path)
" let Vundle manage Vundle, required
Plugin 'gmarik/vundle'
" The following are examples of different formats supported.
" Keep Plugin commands between here and filetype plugin indent on.
" scripts on GitHub repos
" Plugin 'tpope/vim-fugitive'
" Plugin 'Lokaltog/vim-easymotion'
" Plugin 'tpope/vim-rails.git'
" The sparkup vim script is in a subdirectory of this repo called vim.
" Pass the path to set the runtimepath properly.
" Plugin 'rstacruz/sparkup', {'rtp': 'vim/'}
" scripts from
" Plugin 'L9'
" Plugin 'FuzzyFinder'
" scripts not on GitHub
" Plugin 'git://git.wincent.com/command-t.git'
" git repos on your local machine (i.e. when working on your own plugin)
" Plugin ''
" ...
"
filetype plugin indent on " required
" To ignore plugin indent changes, instead use:
" filetype plugin on
"
" Brief help
" : PluginList - list configured plugins
" : PluginInstall(!) - install (update) plugins
" : PluginSearch(!) foo - search (or refresh cache first) for foo
" : PluginClean(!) - confirm (or auto-approve) removal of unused plugins
"
" see :h vundle for more details or wiki for FAQ
" NOTE: comments after Plugin commands are not allowed.
" Put your stuff after this line
syntax on
" ********************************************************************
" 這里省略了其它不相關(guān)的插件
" vimgdb插件
run macros/gdb_mappings.vim
" 官方的插件
" Some Linux distributions set filetype in /etc/vimrc.
" Clear filetype flags before changing runtimepath to force Vim to
" reload them.
filetype off
filetype plugin indent off
set runtimepath+=$GOROOT/misc/vim
filetype plugin indent on
syntax on
autocmd FileType go autocmd BufWritePre buffer Fmt
" 代碼補(bǔ)全的插件
Bundle 'Blackrush/vim-gocode'
" 代碼跳轉(zhuǎn)提示
Bundle 'dgryski/vim-godef'
" 代碼結(jié)構(gòu)提示
Bundle 'majutsushi/tagbar'
nmap F8 :TagbarToggleCR
let g:tagbar_type_go = {
\ 'ctagstype' : 'go',
\ 'kinds' : [
\ 'p:package',
\ 'i:imports:1',
\ 'c:constants',
\ 'v:variables',
\ 't:types',
\ 'n:interfaces',
\ 'w:fields',
\ 'e:embedded',
\ 'm:methods',
\ 'r:constructor',
\ 'f:functions'
\ ],
\ 'sro' : '.',
\ 'kind2scope' : {
\ 't' : 'ctype',
\ 'n' : 'ntype'
\ },
\ 'scope2kind' : {
\ 'ctype' : 't',
\ 'ntype' : 'n'
\ },
\ 'ctagsbin' : 'gotags',
\ 'ctagsargs' : '-sort -silent'
\ }