Golang開發環境搭建-Vim篇


一、一個干凈的環境

找個干凈的基礎環境,方便確認每個搭建步驟后的效果:

Ubuntu 14.04 x86_64
vim version 7.4.52
go version go1.4beta1 linux/amd64

再准備一個編輯Go源碼的測試源文件:

//hellogolang.go

package main

import "fmt"

func main() {
        fmt.Println("Hello Golang!")
}

用於驗證每個搭建步驟后的變化。

二、嚴格按照vim-go的官方說明逐一搭建

Vim-go是當前使用最為廣泛的用於搭建Golang開發環境的vim插件,這里我同樣使用vim-go作為核心和基礎進行環境搭建的。vim-go利 用開源Vim插件管理器安裝,gmarik/Vundle.vim是目前被推薦次數更多的Vim插件管理器,超過了pathogen。這里我們 就用vundle來作為Vim的插件管理工具。

1、安裝Vundle.vim

Vundle.vim的安裝步驟如下:

mkdir ~/.vim/bundle
git clone https://github.com/gmarik/Vundle.vim.git ~/.vim/bundle/Vundle.vim   
                                                                  

創建~/.vimrc文件(如果你沒有這個文件的話),在文件頂部添加有關Vundle.vim的配置:

set nocompatible              " be iMproved, required
filetype off                  " required

" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/vundle/
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

此時Vim僅安裝了Vundle.vim這一個插件。編輯hellogolang.go時與編輯普通文本文件無異,一切都還是Vim的默認屬性。

2、安裝Vim-go

編輯~/.vimrc,在vundle#beginvundle#end間增加一行:

Plugin 'fatih/vim-go'

在Vim內執行  :PluginInstall

Vundle.vim會在左側打開一個Vundle Installer Preview子窗口,窗口下方會提示:“Processing 'fatih/vim-go'”,待安裝完畢后,提示信息變 成“Done!”。

這時,我們可以看到.vim/bundle下多了一個vim-go文件夾:

$ ls .vim/bundle/
vim-go/  Vundle.vim/

此時,再次編輯hellogolang.go,語法高亮有了, 保存時自動format(利用$GOBIN/gofmt)也有了,但其他高級功能,比如自動import缺失的 package、自動補齊仍然沒有,我們還要繼續安裝一些東東。

3、安裝go.tools Binaries

vim-go安裝說明中提到所有必要的binary需要先安裝好,比如gocode、godef、goimports等。

通過:GoInstallBinaries,這些vim-go依賴的二進制工具將會自動被下載,並被安裝到$GOBIN下或$GOPATH/bin下。(這個工具需要依賴git或hg,需要提前安裝到你的OS中。)

:GoInstallBinaries的執行是交互式的,你需要回車確認:

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的自動安裝很可能以失敗告終,這樣就需要你根據上 面日志中提到的各個工具的源碼地址逐一去下載並本地安裝。無法搭梯子的,可以通過http://gopm.io 下載相關包。

安裝后,$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 會彈出補齊提示下拉框,不過並非實時跟隨的那種補齊,這個補齊是由gocode提供的。
    – 輸入一行代碼:time.Sleep(time.Second),執行:GoImports,Vim會自動導入time包。
    – 將光標移到Sleep函數上,執行:GoDef或命令模式下敲入gd,Vim會打開$GOROOT/src/time/sleep.go中 的Sleep函數的定義。執行:b 1返回到hellogolang.go。
    – 執行:GoLint,運行golint在當前Go源文件上。
    – 執行:GoDoc,打開當前光標對應符號的Go文檔。
    – 執行:GoVet,在當前目錄下運行go vet在當前Go源文件上。
    – 執行:GoRun,編譯運行當前main package。
    – 執行:GoBuild,編譯當前包,這取決於你的源文件,GoBuild不產生結果文件。
    – 執行:GoInstall,安裝當前包。
    – 執行:GoTest,測試你當前路徑下地_test.go文件。
    – 執行:GoCoverage,創建一個測試覆蓋結果文件,並打開瀏覽器展示當前包的情況。
    – 執行:GoErrCheck,檢查當前包種可能的未捕獲的errors。
    – 執行:GoFiles,顯示當前包對應的源文件列表。
    – 執行:GoDeps,顯示當前包的依賴包列表。
    – 執行:GoImplements,顯示當前類型實現的interface列表。
    – 執行:GoRename [to],將當前光標下的符號替換為[to]。

三、其他插件

到目前為止,我們還有若干特性沒能實現,重點是:

    – 實時跟隨的代碼補齊
    – Code Snippet support

1、安裝YCM(Your Complete Me)

在~/.vimrc中添加一行:

Plugin 'Valloric/YouCompleteMe'

保存退出后,再打開~/.vimrc並執行 :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++編寫的模塊對性能進行優化了,於是需要手工編譯YCM的support庫。步驟如下:

sudo apt-get install build-essential cmake python-dev
cd ~/.vim/bundle/YouCompleteMe
./install.sh

構建(編譯C++很慢,需要耐心的等一會)ok后,再打開hellogolang.go,逐字的實時補全功能就具備了!Cool!

手動安裝方法

因為安裝過程中ycm一直安裝不成功所以采用手動安裝

git clone https://github.com/Valloric/YouCompleteMe.git

手動下載完后檢查倉庫的完整性,切換到 YouCompleteMe 目錄下,輸入如下命令:

git submodule update --init --recursive

2、安裝 UltiSnips

Vim-go默認是用ultisnips引擎插件,但這個插件需要單獨安裝。

同樣,我們利用vundle來安裝它,在~/.vimrc中添加一行:

Plugin 'SirVer/ultisnips'

snippet和snippet引擎是分開的。ultisnips是引擎,vim-go的go snippet定義在這里

https://github.com/fatih/vim-go/blob/master/gosnippets/snippets/go.snip

編輯hellogolang.go,按照go.snip中的說明,我們輸入func后敲擊tab鍵,我們發現期待的:

func name(params) type {
       
}

並沒有出現。反倒是YCM的下拉提示顯示在那里讓你選擇。似乎是ultisnips和YCM的鍵組合沖突了。ultisnips官方說明也的確如 此。ultisnips默認是用Tab展開snippet的,而YCM中的Tab用來選擇補齊項,我們可以通過設置來避免這些。

我們在.vimrc中添加如下setting:

" YCM settings
let g:ycm_key_list_select_completion = ['', '']
let g:ycm_key_list_previous_completion = ['']
let g:ycm_key_invoke_completion = ''

" UltiSnips setting
let g:UltiSnipsExpandTrigger=""
let g:UltiSnipsJumpForwardTrigger=""
let g:UltiSnipsJumpBackwardTrigger=""

這樣讓YCM通過回車和向下的箭頭來做list item正向選擇,通過向上箭頭做反向選擇。通過ctrl+space來原地觸發補齊提示。

而ultisnips則是用tab做snippet展開,ctrl+b正向切換占位符,ctrl+z反向切換占位符。

3、安裝molokai theme

Molokai theme是TextMate的theme的vim port,看着截圖挺不錯的,於是也安裝了一下。

    mkdir ~/.vim/colors
    下載或copy zip  https://github.com/tomasr/molokai/blob/master/colors/molokai.vim到~/.vim /colors目錄下
    在.vimrc添加一行:colorscheme molokai

四、.vimrc

前面講到了vim-go有許多命令,在:xx模式下執行多顯不便,於是你可以定義一些Mappings,比如:

" set mapleader
let mapleader = ","

" vim-go custom mappings
au FileType go nmap s (go-implements)
au FileType go nmap i (go-info)
au FileType go nmap gd (go-doc)
au FileType go nmap gv (go-doc-vertical)
au FileType go nmap r (go-run)
au FileType go nmap b (go-build)
au FileType go nmap t (go-test)
au FileType go nmap c (go-coverage)
au FileType go nmap ds (go-def-split)
au FileType go nmap dv (go-def-vertical)
au FileType go nmap dt (go-def-tab)
au FileType go nmap e (go-rename)

這樣我們在命令模式下,輸入<,>+就是運行 當前main包,以此類推。

另外下面這個配置使得我們在save file時既可以格式化代碼,又可以自動插入包導入語句(或刪除不用的包導入語句)。

" vim-go settings
let g:go_fmt_command = "goimports"

到這里,我們的Vim Golang開發環境就基本搭建好了。snippet+實時補齊讓你Coding如飛!

五、附錄:.vimrc文件

下面是截至目前為止全量.vimrc文件的內容:

set nocompatible              " be iMproved, required
filetype off                  " required
colorscheme molokai

" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/vundle/
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 s (go-implements)
au FileType go nmap i (go-info)
au FileType go nmap gd (go-doc)
au FileType go nmap gv (go-doc-vertical)
au FileType go nmap r (go-run)
au FileType go nmap b (go-build)
au FileType go nmap t (go-test)
au FileType go nmap c (go-coverage)
au FileType go nmap ds (go-def-split)
au FileType go nmap dv (go-def-vertical)
au FileType go nmap dt (go-def-tab)
au FileType go nmap e (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 = ''

" UltiSnips settings
let g:UltiSnipsExpandTrigger=""
let g:UltiSnipsJumpForwardTrigger=""
let g:UltiSnipsJumpBackwardTrigger=""


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM