1. 程式人生 > >Vim go語言開發環境配置

Vim go語言開發環境配置

安裝vim 和 vim-go 

sudo apt-get install vim
git clone https://github.com/fatih/vim-go.git ~/.vim/bundle/vim-go

安裝Vundle

sudo apt-get install git
sudo apt-get install curl

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

編輯配置檔案

vim ~/.vimrc

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'
Plugin 'fatih/vim-go'
"https://github.com/fatih/vim-go
Plugin 'https://github.com/Valloric/YouCompleteMe'

" " All of your Plugins must be added before the following line
call vundle#end()            " required
filetype plugin indent on    " required

set number
set autoindent
set shiftwidth=4
set ts=4
set expandtab
set softtabstop=4
set paste

set fileencodings=utf-8,gb2312,gb18030,gbk,ucs-bom,cp936,latin1
set termencoding=utf-8
set fileformats=unix
set encoding=utf-8

儲存後再次開啟vim,輸入 PluginInstall ,會出現一個子視窗,等待下面出現Done!

cd ~/.vim/bundle ,這時候可以進入 Vundle 目錄看到出現了 vim-go 目錄。

安裝Go外掛專案

vim ~/.vim/bundle/vim-go/plugin/go.vim

配置檔案裡面的 s:packages 要注意,由於某種原因無法訪問某些,所以從 https://www.golangtc.com/download/package 下載後到 GOPATH/src 裡,然後cd進入專案目錄,進行 go build 生成可執行檔案再放在GOPATH/bin 下。

一般來說package main 的都在專案根目錄,也有特例比如說 golint 要到 目錄下的 golint 進行編譯才能生成可執行檔案。

開啟 vim 輸入 GoInstallBinaries 就開始安裝了,注意看提示資訊,缺哪個就編譯哪個放進去。

再次編輯任意的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]。

安裝實時程式碼提示

上面配置檔案中已經加上了  Plugin 'https://github.com/Valloric/YouCompleteMe'  ,如果新輸入的話儲存退出,再開啟Vim輸入PluginInstall。

apt-get install build-essential cmake python-dev

cd ~/.vim/bundle/ 
git clone https://github.com/Valloric/YouCompleteMe.git
cd YouCompleteMe/
git submodule update --init --recursive

cd ~/.vim/bundle/YouCompleteMe
/install.py --gocode-completer