Wednesday, March 9, 2016




之前文章有談到sublime寫Golang 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
  % 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



安裝git apt-get install git

git clone ~/.vim/bundle/Vundle.vim


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/

The package will be installed to GOPATH/bin directory.

And you will see

vim-go: gocode not found. Installing to folder /usr/local/go/src//b
vim-go: gometalinter not found. Installing to folder /



package main

import "fmt"

func main() {
    print("hello, world\n")

after . try the following command, you will see the auto-completion.

ctrl-x ctrl-o

Adding Persistent Setting

GOPATH Setting

edit /etc/profile.d/

root@golang15env# cat /etc/profile.d/ 
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


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


Now we can reboot the OS and you will found the setting is persistent.

Adding library

go get

you will see the library

root@golang15env:# ls ../../pkg/linux_amd64/
context.a  mux.a

We modify our code.

package main

import (

func main() {
    print("hello, world\n")

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/ /usr/local/go/pkg/linux_amd64/ -rf

Now you can test c-x c-o again.

Build Code

see this

Advanced Skill

Great Video.

trace code


git clone git://
cd gotags/src/
go build *
cp gotags /usr/local/bin


gotags *


ctrl-]  進入下一層
ctrl-o  回到上一層

好了,Auto-completion,color,trace code,全部設定完畢。

No comments:

Post a Comment