Golang Development Environment Build-vim Chapter

Source: Internet
Author: User
Tags install go sleep function gocode
This is a creation in Article, where the information may have evolved or changed.

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 ~/.VIMRC to add a line between Vundle#begin and Vundle#end:

Plugin ' Fatih/vim-go '

Execute the Lugininstall within 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 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 binary tools will be downloaded automatically and installed under $gobin or $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.go again:

-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 = "

"Ultisnips setting
Let g:ultisnipsexpandtrigger= " "
Let g:ultisnipsjumpforwardtrigger= " "
Let g:ultisnipsjumpbackwardtrigger= " "

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 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 g o nmap dv (go-def-vertical)
au FileType go nmap DT (go-def-tab)
au FileType go nmap e (go-rename)

So in command mode, the input <,>+ 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 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 g o 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= " "

, Bigwhite. All rights reserved.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.