Golang的VI環境設定
之前文章有談到sublime寫Golang code,
http://gogosatellite.blogspot.tw/2016/01/gosub-span-display-block-overflow.html
http://gogosatellite.blogspot.tw/2016/02/golang.html
現在我們來試試看VI。
一直以來我都是用VI寫code,原因是,手可以不用離開鍵盤。
原本以為讓Golang在VI的環境下完成,Auto-completion,color,trace code,會很困難。
沒想到,出乎意料的簡單。
UPdate VIM
For Ubuntu14.04 Origin Vim Version is 7.4.52 that cannot support Golang plugin well. So before we started to install Golang, let's update vim first.
add-apt-repository ppa:pkg-vim/vim-daily
apt-get update
apt-get install vim
Now the vim upgrade to version 7.4.882
以下開始安裝
root@golang15:~# curl -O https://storage.googleapis.com/golang/go1.5.linux-amd64.tar.gz
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 74.2M 100 74.2M 0 0 4811k 0 0:00:15 0:00:15 --:--:-- 5183k
tar -zxvf go1.5.linux-amd64.tar.gz
mv -f go /usr/local/
root@golang15:~# go version
The program 'go' is currently not installed. You can install it by typing:
apt-get install gccgo-go
However, do not install it. It's just due to GO PATH is not setting well.
Now we adding go path to system PATH.
root@golang15:~# export PATH=$PATH:/usr/local/go/bin
root@golang15:~# go version
go version go1.5 linux/amd64
Now it works.
Let's try a simple Golang Code.
root@golang15:~# cp /usr/local/go/test/helloworld.go .
root@golang15:~# go run helloworld.go
hello, world
Great.
以下開始設定VIM開發環境
安裝git
apt-get install git
git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
編輯.vimrc
set nocompatible
syntax on
filetype off
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
Plugin 'VundleVim/Vundle.vim'
Plugin 'fatih/vim-go'
Plugin 'kien/ctrlp.vim'
Plugin 'scrooloose/nerdtree'
Plugin 'Townk/vim-autoclose'
Plugin 'SirVer/ultisnips'
call vundle#end()
filetype plugin indent on
au BufNewFile,BufRead *.go setlocal noet ts=4 sw=4 sts=4
if has("autocmd")
autocmd BufRead *.txt set tw=78
autocmd BufReadPost *
\ if line("'\"") > 0 && line ("'\"") <= line("$") |
\ exe "normal g'\"" |
\ endif
endif ""'")
let g:NERDTreeDirArrows=0
" UltiSnips setting
let g:UltiSnipsExpandTrigger="<tab>"
let g:UltiSnipsJumpForwardTrigger="<c-b>"
let g:UltiSnipsJumpBackwardTrigger="<c-z>"
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)
接著在console下執行VI Plugin的安裝
Started install Plugin setting in .vim
vim +PluginInstall +qall
You will see all the package installed in .vim/bundle
directory.
Now we install it.
export GOPATH=/usr/local/go/src/
vim
:GoInstallBinaries
The package will be installed to GOPATH
/bin directory.
And you will see
vim-go: gocode not found. Installing github.com/nsf/gocode to folder /usr/local/go/src//b
in/
vim-go: gometalinter not found. Installing github.com/alecthomas/gometalinter to folder /
usr/local/go/src//bin/
.
.
.
.
InstallBinary會稍微慢些,要有點耐心,會執行完的。
上述簡單幾個操作,基本上,你已完成了所有的設定,你可以找個函數試試看
package main
import "fmt"
func main() {
print("hello, world\n")
fmt.
}
after .
try the following command, you will see the auto-completion.
ctrl-x ctrl-o
Adding Persistent Setting
GOPATH Setting
edit /etc/profile.d/golang.sh
root@golang15env# cat /etc/profile.d/golang.sh
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
export GOPATH=/root/golang/projects/wru
export GOBIN=$GOPATH/bin
export GOARCH=amd64
export GOOS=linux
Problems
The problem is we set GOPATH
is in the /usr/local/go/src
, and all the VIM
is installed
in GOPATH
/bin.
Now we need to copy all the vim
data to working directory.
cp /usr/local/go/src/bin $GOPATH/bin
Reboot
Now we can reboot the OS and you will found the setting is persistent.
Adding library
go get github.com/gorilla/mux
you will see the library
root@golang15env:# ls ../../pkg/linux_amd64/github.com/gorilla/
context.a mux.a
We modify our code.
package main
import (
"fmt"
"github.com/gorilla/mux"
)
func main() {
print("hello, world\n")
fmt.Println("aaa")
mux.
}
auto-complete the mux.
, it's failed.
The way to do it is to copy the library to GOROOT related directory
cp ../../../pkg/linux_amd64/github.com/gorilla /usr/local/go/pkg/linux_amd64/github.com/. -rf
Now you can test c-x c-o
again.
Build Code
see this
http://gogosatellite.blogspot.tw/2016/02/golang.html
Advanced Skill
Great Video.
https://www.youtube.com/watch?v=7BqJ8dzygtU&t=5s
trace code
以前用C語言的時候用ctags,沒想到golang有gotags,而且安裝與使用都非常簡單。
首先下載gotags的源碼
git clone git://github.com/jimweirich/gotags.git
cd gotags/src/onestepback.org/gotags/
go build *
cp gotags /usr/local/bin
這時你可以到你要做tags的目錄去執行
gotags *
用vi開啟你要trace的代碼,並透過下列兩行指令可以輕鬆追代碼。
ctrl-] 進入下一層
ctrl-o 回到上一層
好了,Auto-completion,color,trace code,全部設定完畢。
可以用VI寫Golang了。
No comments:
Post a Comment