一、環(huán)境準(zhǔn)備:

網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)!專注于網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、微信小程序定制開發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了海南州免費(fèi)建站歡迎大家使用!
系統(tǒng)環(huán)境說明:
[root@docker golang]# cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core) [root@docker golang]# uname -a Linux docker 3.10.0-229.el7.x86_64 #1 SMP Fri Mar 6 11:36:42 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux [root@docker golang]#
準(zhǔn)備一個(gè)go文件,用于觀察配置插件過程中的變化:
//hellogolang.go
package main
import "fmt"
func main() {
fmt.Println("Hello Golang!")
}二、插件配置之路:
1、Vundle.vim:
#mkdir ~/.vim/bundle #git clone https://github.com/gmarik/Vundle.vim.git ~/.vim/bundle/Vundle.vim
配置vimrc:創(chuàng)建~/.vimrc文件(如果你沒有這個(gè)文件的話),在文件頂部添加有關(guān)Vundle.vim的配置:
set nocompatible " be iMproved, required filetype off " required " set the runtime path to include Vundle and initialize set rtp+=~/.vim/bundle/Vundle.vim call vundle#begin() " let Vundle manage Vundle, required Plugin 'gmarik/Vundle.vim' " All of your Plugins must be added before the following line call vundle#end() " required filetype plugin indent on " required
此時(shí)Vim僅安裝了Vundle.vim這一個(gè)插件。編輯hellogolang.go時(shí)與編輯普通文本文件無異,一切都還是Vim的默認(rèn)屬性
2、vim-go
Vim-go是當(dāng)前使用最為廣泛的用于搭建Golang開發(fā)環(huán)境的vim插件,這里我同樣使用vim-go作為核心和基礎(chǔ)進(jìn)行環(huán)境搭建的。vim-go利 用開源Vim插件管理器安裝,gmarik/Vundle.vim是目前被推薦次數(shù)更多的Vim插件管理器,超過了pathogen。
這里我們 就用vundle來作為Vim的插件管理工具。
編輯~/.vimrc,在vundle#begin和vundle#end間增加一行:
Plugin 'fatih/vim-go'
在Vim內(nèi)執(zhí)行:PluginInstall,
Vundle.vim會(huì)在左側(cè)打開一個(gè)Vundle Installer Preview子窗口,窗口下方會(huì)提示:“Processing 'fatih/vim-go'”,待安裝完畢后,提示信息變 成“Done!”。
這時(shí),我們可以看到.vim/bundle下多了一個(gè)vim-go文件夾:
$ ls .vim/bundle/ vim-go/ Vundle.vim/
此時(shí),再次編輯hellogolang.go,語(yǔ)法高亮有了, 保存時(shí)自動(dòng)format(利用$GOBIN/gofmt)也有了,但其他高級(jí)功能,比如自動(dòng)import缺失的 package、自動(dòng)補(bǔ)齊仍然沒有,我們還要繼續(xù)安裝一些東東。
3、安裝go.tools Binaries
vim-go安裝說明中提到所有必要的binary需要先安裝好,比如gocode、godef、goimports等。
通過:GoInstallBinaries,這些vim-go依賴的二進(jìn)制工具將會(huì)自動(dòng)被下載,并被安裝到$GOBIN下或$GOPATH/bin下。(這個(gè)工具需要依賴git或hg,需要提前安裝到你的OS中。)
:GoInstallBinaries的執(zhí)行是交互式的,你需要回車確認(rèn):
vim-go: gocode not found. Installing github.com/nsf/gocode to folder /home/tonybai/go/bin vim-go: goimports not found. Installing code.google.com/p/go.tools/cmd/goimports to folder /home/tonybai/go/bin/ vim-go: godef not found. Installing code.google.com/p/rog-go/exp/cmd/godef to folder /home/tonybai/go/bin/ vim-go: oracle not found. Installing code.google.com/p/go.tools/cmd/oracle to folder /home/tonybai/go/bin/ vim-go: gorename not found. Installing code.google.com/p/go.tools/cmd/gorename to folder /home/tonybai/go/bin/ vim-go: golint not found. Installing github.com/golang/lint/golint to folder /home/tonybai/go/bin/ vim-go: errcheck not found. Installing github.com/kisielk/errcheck to folder /home/tonybai/go/bin/
不過這些代碼多在code.google.com上托管,因此由于眾所周知的原因,vim-go的自動(dòng)安裝很可能以失敗告終,這樣就需要你根據(jù)上 面日志中提到的各個(gè)工具的源碼地址逐一去下載并本地安裝。無法搭梯子的,可以通過http://gopm.io 下載相關(guān)包。
安裝后,$GOBIN下的新增Binaries如下:
-rwxr-xr-x 1 tonybai tonybai 5735552 11?? 7 11:03 errcheck* -rwxr-xr-x 1 tonybai tonybai 9951008 11?? 7 10:33 gocode* -rwxr-xr-x 1 tonybai tonybai 5742800 11?? 7 11:07 godef* -rwxr-xr-x 1 tonybai tonybai 4994120 11?? 7 11:00 goimports* -rwxr-xr-x 1 tonybai tonybai 5750152 11?? 7 11:03 golint* -rwxr-xr-x 1 tonybai tonybai 6381832 11?? 7 11:01 gorename* -rwxr-xr-x 1 tonybai tonybai 2954392 11?? 7 10:38 gotags* -rwxr-xr-x 1 tonybai tonybai 9222856 11?? 7 11:01 oracle*
安裝好這些Binaries后,我們來看看哪些特性被支持了。
再次編輯hellogolang.go:
- 新起一行輸入fmt.,然后ctrl+x, ctrl+o,Vim 會(huì)彈出補(bǔ)齊提示下拉框,不過并非實(shí)時(shí)跟隨的那種補(bǔ)齊,這個(gè)補(bǔ)齊是由gocode提供的。
– 輸入一行代碼:time.Sleep(time.Second),執(zhí)行:GoImports,Vim會(huì)自動(dòng)導(dǎo)入time包。
– 將光標(biāo)移到Sleep函數(shù)上,執(zhí)行:GoDef或命令模式下敲入gd,Vim會(huì)打開$GOROOT/src/time/sleep.go中 的Sleep函數(shù)的定義。執(zhí)行:b 1返回到hellogolang.go。
– 執(zhí)行:GoLint,運(yùn)行g(shù)olint在當(dāng)前Go源文件上。
– 執(zhí)行:GoDoc,打開當(dāng)前光標(biāo)對(duì)應(yīng)符號(hào)的Go文檔。
– 執(zhí)行:GoVet,在當(dāng)前目錄下運(yùn)行g(shù)o vet在當(dāng)前Go源文件上。
– 執(zhí)行:GoRun,編譯運(yùn)行當(dāng)前main package。
– 執(zhí)行:GoBuild,編譯當(dāng)前包,這取決于你的源文件,GoBuild不產(chǎn)生結(jié)果文件。
– 執(zhí)行:GoInstall,安裝當(dāng)前包。
– 執(zhí)行:GoTest,測(cè)試你當(dāng)前路徑下地_test.go文件。
– 執(zhí)行:GoCoverage,創(chuàng)建一個(gè)測(cè)試覆蓋結(jié)果文件,并打開瀏覽器展示當(dāng)前包的情況。
– 執(zhí)行:GoErrCheck,檢查當(dāng)前包種可能的未捕獲的errors。
– 執(zhí)行:GoFiles,顯示當(dāng)前包對(duì)應(yīng)的源文件列表。
– 執(zhí)行:GoDeps,顯示當(dāng)前包的依賴包列表。
– 執(zhí)行:GoImplements,顯示當(dāng)前類型實(shí)現(xiàn)的interface列表。
– 執(zhí)行:GoRename [to],將當(dāng)前光標(biāo)下的符號(hào)替換為[to]。
三、其他插件
到目前為止,我們還有若干特性沒能實(shí)現(xiàn),重點(diǎn)是:
– 實(shí)時(shí)跟隨的代碼補(bǔ)齊
– Code Snippet support
1、安裝YCM(Your Complete Me)
在~/.vimrc中添加一行:
Plugin 'Valloric/YouCompleteMe'
保存退出后,再打開~/.vimrc并執(zhí)行:PluginInstall
安裝完后,下面的提示欄提示:
ycm_client_support.[so|pyd|dll] and ycm_core.[so|pyd|dll] not detected; you need to compile YCM before using it. Read the docs!
YCM是用了C++編寫的模塊對(duì)性能進(jìn)行優(yōu)化了,于是需要手工編譯YCM的support庫(kù)。步驟如下:
sudo yum install build-essential cmake python-dev cd ~/.vim/bundle/YouCompleteMe ./install.sh
構(gòu)建(編譯C++很慢,需要耐心的等一會(huì))ok后,再打開hellogolang.go,逐字的實(shí)時(shí)補(bǔ)全功能就具備了!Cool!
2、安裝 UltiSnips
Vim-go默認(rèn)是用ultisnips引擎插件,但這個(gè)插件需要單獨(dú)安裝。同樣,我們利用vundle來安裝它,在~/.vimrc中添加一行:
Plugin 'SirVer/ultisnips'
保存,退出vim
打開vim,執(zhí)行:PluginInstall
編輯hellogolang.go,按照go.snip中的說明,我們輸入func后敲擊tab鍵,我們發(fā)現(xiàn)期待的:
func name(params) type {
}并沒有出現(xiàn)。反倒是YCM的下拉提示顯示在那里讓你選擇。似乎是ultisnips和YCM的鍵組合沖突了。ultisnips官方說明也的確如 此。ultisnips默認(rèn)是用Tab展開snippet的,而YCM中的Tab用來選擇補(bǔ)齊項(xiàng),我們可以通過設(shè)置來避免這些。
我們?cè)?vimrc中添加如下setting:
" YCM settings let g:ycm_key_list_select_completion = ['', ''] let g:ycm_key_list_previous_completion = [''] let g:ycm_key_invoke_completion = '<C-Space>' " UltiSnips setting let g:UltiSnipsExpandTrigger="<tab>" let g:UltiSnipsJumpForwardTrigger="<c-b>" let g:UltiSnipsJumpBackwardTrigger="<c-z>"
這樣讓YCM通過回車和向下的箭頭來做list item正向選擇,通過向上箭頭做反向選擇。通過ctrl+space來原地觸發(fā)補(bǔ)齊提示。
而ultisnips則是用tab做snippet展開,ctrl+b正向切換占位符,ctrl+z反向切換占位符。
四、.vimrc
前面講到了vim-go有許多命令,在:xx模式下執(zhí)行多顯不便,于是你可以定義一些Mappings,比如:
" set mapleader let mapleader = "," " vim-go custom mappings au FileType go nmap <Leader>s <Plug>(go-implements) au FileType go nmap <Leader>i <Plug>(go-info) au FileType go nmap <Leader>gd <Plug>(go-doc) au FileType go nmap <Leader>gv <Plug>(go-doc-vertical) au FileType go nmap <leader>r <Plug>(go-run) au FileType go nmap <leader>b <Plug>(go-build) au FileType go nmap <leader>t <Plug>(go-test) au FileType go nmap <leader>c <Plug>(go-coverage) au FileType go nmap <Leader>ds <Plug>(go-def-split) au FileType go nmap <Leader>dv <Plug>(go-def-vertical) au FileType go nmap <Leader>dt <Plug>(go-def-tab) au FileType go nmap <Leader>e <Plug>(go-rename)
這樣我們?cè)诿钅J较拢斎?lt;,>+<r>就是運(yùn)行 當(dāng)前main包,以此類推。
另外下面這個(gè)配置使得我們?cè)趕ave file時(shí)既可以格式化代碼,又可以自動(dòng)插入包導(dǎo)入語(yǔ)句(或刪除不用的包導(dǎo)入語(yǔ)句)。
" vim-go settings let g:go_fmt_command = "goimports"
到這里,我們的Vim Golang開發(fā)環(huán)境就基本搭建好了。snippet+實(shí)時(shí)補(bǔ)齊讓你Coding如飛!
五、.vimrc文件
下面是截至目前為止全量.vimrc文件的內(nèi)容: set nocompatible " be iMproved, required filetype off " required colorscheme molokai " set the runtime path to include Vundle and initialize set rtp+=~/.vim/bundle/Vundle.vim call vundle#begin() " let Vundle manage Vundle, required Plugin 'gmarik/Vundle.vim' Plugin 'fatih/vim-go' Plugin 'Valloric/YouCompleteMe' Plugin 'SirVer/ultisnips' " All of your Plugins must be added before the following line call vundle#end() " required filetype plugin indent on " required " set mapleader let mapleader = "," " vim-go custom mappings au FileType go nmap <Leader>s <Plug>(go-implements) au FileType go nmap <Leader>i <Plug>(go-info) au FileType go nmap <Leader>gd <Plug>(go-doc) au FileType go nmap <Leader>gv <Plug>(go-doc-vertical) au FileType go nmap <leader>r <Plug>(go-run) au FileType go nmap <leader>b <Plug>(go-build) au FileType go nmap <leader>t <Plug>(go-test) au FileType go nmap <leader>c <Plug>(go-coverage) au FileType go nmap <Leader>ds <Plug>(go-def-split) au FileType go nmap <Leader>dv <Plug>(go-def-vertical) au FileType go nmap <Leader>dt <Plug>(go-def-tab) au FileType go nmap <Leader>e <Plug>(go-rename) " vim-go settings let g:go_fmt_command = "goimports" " YCM settings let g:ycm_key_list_select_completion = ['', ''] let g:ycm_key_list_previous_completion = ['', ''] let g:ycm_key_invoke_completion = '<C-Space>' " UltiSnips settings let g:UltiSnipsExpandTrigger="<tab>" let g:UltiSnipsJumpForwardTrigger="<c-b>" let g:UltiSnipsJumpBackwardTrigger="<c-z>"
以上就是go語(yǔ)言環(huán)境vim配置詳解的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注創(chuàng)新互聯(lián)其它相關(guān)文章!
當(dāng)前標(biāo)題:golang如何配置vim環(huán)境
文章轉(zhuǎn)載:http://www.chinadenli.net/article6/piieig.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、自適應(yīng)網(wǎng)站、全網(wǎng)營(yíng)銷推廣、商城網(wǎng)站、網(wǎng)站營(yíng)銷、網(wǎng)站改版
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)