Golang Development Environment Build-vim Chapter
- Transferred from: http://tonybai.com/2014/11/07/golang-development-environment-for-vim/
Although Sublimetext3+gosublime+gocode is currently a more popular Golang development environment combination, but as a vimer, not a set of handy vim for Golang Dev heart always get through. Golang is young, but even from the release of Go 1 (March 28, 2012), it has been a small three years since the pinch was counted. Developers around the world have contributed more mature vim plugins to Golang. With these plug-ins, to build a set of efficient Golang development environment is not difficult, there is a large number of online information can be consulted, including Vim-go author himself published an article "Go development environment for Vim." However, to see what others write and build their own experience is very different, so want to go or the whole process of recording down.
One clean environment
Find a clean basic environment that allows you to confirm the effect of each build step:
Ubuntu 14.04 x86_64
Vim version 7.4.52
Go version go1.4beta1 linux/amd64
Then prepare a test source file to edit the go source:
Hellogolang.go
Package Main
Import "FMT"
Func Main () {
Fmt. Println ("Hello golang!")
}
Used to verify the changes after each build step.
Second, in strict accordance with the official instructions of Vim-go to build
Vim-go is currently the most widely used vim plugin for building the Golang development environment, where I also use vim-go as the core and foundation for Environment building. Vim-go is installed using the open source Vim plug-in manager, Gmarik/vundle.vim is currently more than the recommended number of Vim plugin manager, over the pathogen. Here we use Vundle as a plugin management tool for Vim.
1, installation Vundle.vim
The installation steps for the Vundle.vim are as follows:
mkdir ~/.vim/bundle
git clone https://github.com/gmarik/Vundle.vim.git ~/.vim/bundle/vundle.vim
To create the ~/.vimrc file (if you don't have one), add the configuration about Vundle.vim at the top of the file:
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 is added before the following line
Call Vundle#end () "Required
FileType plugin indent on "required
At this point vim only installs the Vundle.vim plugin. Editing hellogolang.go is no different than editing a plain text file, and everything is the default property of Vim.
2, Installation Vim-go
Edit ~/.VIMRCto add a line between Vundle#begin and vundle#end :
Plugin ' Fatih/vim-go '
Execute the lugininstallwithin Vim,
Vundle.vim will open a Vundle Installer Preview Sub-window on the left, the window will prompt:"processing ' fatih/vim-go '", after the installation is complete, the prompt message becomes "done !”。
At this point, we can see. vim/bundle a Vim-go folder:
$ ls. vim/bundle/
vim-go/vundle.vim/
At this point, edit the Hellogolang.go again, syntax highlighting , save the Automatic format(using $GOBIN/GOFMT) also has, but other advanced features, such as the automatic import missing package, Auto-Completion still no, we have to continue to install some of the stuff.
3, install Go.tools Binaries
Vim-go Installation instructions mentioned that all necessary binary needs to be installed first, such as Gocode, Godef, Goimports and so on.
by : Goinstallbinaries, these vim-go dependent binaries will be downloaded automatically and installed under $GOBIN or under the $GOPATH/bin . (This tool relies on Git or Hg and needs to be installed into your OS in advance.) )
: goinstallbinaries execution is interactive and you need to enter to confirm:
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/
However, these codes are hosted on code.google.com, so for well-known reasons, Vim-go's automatic installation is likely to fail, which requires you to download and install it locally based on the source code of each tool mentioned in the log above. If you can't get a ladder, you can download the relevant package via Http://gopm.io.
After installation, the new binaries under $GOBIN are as follows:
-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:00am 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*
After installing these binaries, let's see which features are supported.
Edit hellogolang.goagain:
-A new line of input FMT, and then Ctrl+x, Ctrl+o,vim will pop up the fill hint drop-down box, but not real-time follow the kind of completion, this complement is provided by Gocode.
– Enter a line of code: time. Sleep (time. Second), execute: Goimports,vim will automatically import the time package.
– Move the cursor over the sleep function, execute: GODEF or Command mode Gd,vim opens the definition of the sleep function in $goroot/src/time/sleep.go. Execution: B 1 return to Hellogolang.go.
– Execute: Golint, run Golint on the current go source file.
– Execute: Godoc, open the Go document with the current cursor corresponding symbol.
– Execute: Govet, run go vet in the current directory on the current go source file.
– Execute: GoRun, compile and run the current main package.
– Execute: Gobuild, compile the current package, depending on your source file, Gobuild does not produce the resulting file.
– Execute: Goinstall, install the current package.
– Execute: gotest, test your current path to the _test.go file.
– Execute: gocoverage, create a test overlay result file, and open the browser to show the current package situation.
– Execute: Goerrcheck, check for possible uncaught errors of the current package type.
– Execute: Gofiles, displays the list of source files for the current package.
– Execute: Godeps, displays the list of dependent packages for the current package.
– Execute: goimplements, displays the list of interface for the current type implementation.
– Execute: gorename [to], replacing the symbol under the current cursor with [to].
Third, other plug-ins
So far, there have been a number of features that have not been implemented, focusing on:
– Code completion with real-time follow
–code Snippet Support
1. Install ycm (Your complete Me)
Add a line in ~/.VIMRC:
Plugin ' Valloric/youcompleteme '
After you save the exit, open ~/.VIMRC and execute lugininstall.
After the installation is complete, the following prompt bar prompts:
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!
It seems that ycm is using a module written in C + + to optimize performance, so you need to manually compile the YCM support library. The steps are as follows:
sudo apt-get install build-essential cmake Python-dev
CD ~/.vim/bundle/youcompleteme
./install.sh
Build (compile C + + very slow, need to wait a while) OK, then open hellogolang.go, verbatim real-time full function to have! cool!
2. Installation ultisnips
The vim-go default is to use the Ultisnips engine plug-in, but this plugin needs to be installed separately.
Again, we use Vundle to install it and add a line to the ~/.VIMRC:
Plugin ' Sirver/ultisnips '
The snippet and snippet engines are separate. Ultisnips is the engine, Vim-go's go snippet is defined here
Https://github.com/fatih/vim-go/blob/master/gosnippets/snippets/go.snip
To edit hellogolang.go, follow the instructions in Go.snip, we enter Func and then hit the TAB key, we find the expected:
Func name (params) type {
}
did not appear. Instead, YCM's drop-down hint shows you where to choose. It seems that ultisnips and ycm have conflicting key combinations. Ultisnips official note that is true. Ultisnips By default is tab-expanded snippet, and tab in YCM is used to select a complement, which we can avoid by setting.
We add the following setting in. VIMRC:
"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>"
This allows the ycm to make a list item forward selection by a carriage return and a downward arrow, which is reversed by the up arrow. Trigger the completion prompt by Ctrl+space.
And ultisnips is to use tab to do snippet expand, ctrl+b forward switch placeholder, CTRL + Z reverse switch placeholder.
3, install Molokai theme
Molokai theme is TextMate's theme vim port, looked very good, so also installed a bit.
mkdir ~/.vim/colors
Download or copy Https://github.com/fatih/molokai/blob/master/colors/molokai.vim to the ~/.vim/colors directory
Add a line to the. VIMRC: ColorScheme Molokai
Iv.. VIMRC
As mentioned earlier Vim-go has many commands, in: xx mode execution more inconvenient, so you can define some mappings, such as:
The 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)
So in command mode, input <,>+<r> is running the current main package, and so on.
In addition, the following configuration allows us to format the code while saving the file, and to automatically insert the package import statement (or delete the unused package import statement).
"Vim-go Settings
Let G:go_fmt_command = "Goimports"
Here, our VIM Golang development environment is basically built. snippet+ real-time to make you coding fly!
V. Appendix:. vimrc File
The following is the contents of the full-volume. vimrc file so far:
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 is added before the following line
Call Vundle#end () "Required
FileType plugin indent on "required
The 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>"
Six, Mac OS x VIM Configuration
1. Macvim Replacement
The configuration method under Mac OS X is slightly different, because the system comes with Vim under Mac is version 7.3, YCM requires vim 7.3.584+ version, so we need to install Macvim to replace our own vim, currently macvim the latest version 7.4.258, fully meet the requirements. Here https://github.com/b4winckler/macvim/releases can download to the latest macvim, after the download Macvim can replace the original VIM by the following steps.
The original Vim is installed under the/usr/bin/vim.
Macvim after decompression as follows:
[[email protected] ~/downloads/macvim-snapshot-73] $ls
Macvim.app/readme.txt mvim*
We do the following steps to complete the VIM replacement work:
sudo mv/usr/bin/vim/usr/bin/vim.bak//Back up the original vim
CP mvim/usr/local/bin/
sudo ln-s/usr/local/bin/mvim/usr/bin/vim
2. Plug-in installation and configuration
According to the above Linux vim plug-in installation steps and configuration method we configure Macvim, after configuration, we found that in addition to Molokai colorscheme not effective, the rest of the plug-in work is normal. All. Go files are open without the color highlighting of the Molokai scheme, and even the general color highlighting is not. After continuous debugging, found a solution, add a few lines of code in ~/.VIMRC :
Syntax on
Au bufread,bufnewfile *.go set Filetype=go
ColorScheme Molokai
But if these lines of configuration code were placed in front of ~/.VIMRC, then Ultisnips would not work, I would move it to the end of the ~/.VIMRC file, so there would be no conflict (it seems. The sequencing of the plug-in configuration of VIMRC will have an impact on the normal use of plug-in functionality). The beautiful Molokai ColorScheme will also show up!
? , Bigwhite. All rights reserved.
Golang Development Environment Build-vim Chapter