Build a go development environment based on VIM

Source: Internet
Author: User
Tags install go gocode gotags

Go as a new language, development in full swing AH. However, there is not yet a mature IDE. This paper introduces the development environment of the go based on vim, which ensures the pleasure of vim while supporting the features of Go.

1. Go environment

The first is to install go. Download the installation package, unzip it, and then set the environment variable

Goroot: points to the go path after decompression.

Gopath: Points to the workspace for development. Go organizes the folder in a fixed file structure, makes it easy to publish code, or download code from another repository. You need to create a src/,pkg/,bin/three folder in this directory

All executable programs generated by Path:go are stored under $gopath/bin and need to be added to the path. Also, you need to add the Go tool to path, which is $goroot/bin.

After completion, you can simply write a go program under SRC to test if the installation is successful. The file path: $GOPATH/src/chosen0ne.com/hello.go

Package Mainimport "FMT" Func Main () {FMT. Printf ("Hello, world.\n")}
Run the following command to execute the program:

Go Run chosen0ne.com/hello.go
If there is an output, the installation is successful.

2. Vim's Go Plugin

Under $goroot/misc/vim, there are vim plugins that support go-related features, including: syntax highlighting, indenting, godoc support, and more. It is described in Readme.txt in the following manner. The main is to add the following code to the. VIMRC:

  "Some Linux distributions set filetype IN/ETC/VIMRC.  " Clear filetype flags before changing runtimepath to force Vim to reload them.  If exists ("G:did_load_filetypes")    filetype off    filetype plugin indent off  endif  set runtimepath+=$ Goroot/misc/vim "Replace $GOROOT with the output of:go env goroot  filetype plugin indent on  syntax on

Then: Source ~/.VIMRC, open a go file, you will see syntax highlighting.


3. Vim Tag list for Go

This plugin lists the variables, types, functions, etc. in the go file and supports jumps.

Need to install Tagbar plug-in, itself this plugin relies on exuberant ctags, but it does not support go, need to pass gotags.

1) Install Gotags

Gotags is written by go, the installation is simple and runs directly:

Go get-u github.com/jstemmer/gotags
The gotags will be installed into the $goroot/bin.

2) Installation Configuration Tagbar

Tagbar plug-ins and common vim plug-in installation, can be through Pathtogen or vundle. After installation, you need to configure go and add the following code to the. VIMRC:

Let G:tagbar_type_go = {    \ ' ctagstype ': ' Go ',    \ ' kinds '     : [        \ ' p:package ',        \ ' i:imports:1 ',        \ ' C : Constants ',        \ ' v:variables ',        \ ' t:types ',        \ ' n:interfaces ',        \ ' w:fields ',        \ ' e:embedded ',        \ ' m:methods ',        \ ' r:constructor ',        \ ' f:functions '    \],    \ ' SRO ': '. ', '    kind2scope ': { C17/>\ ' t ': ' CType ',        \ ' n ': ' Ntype '    \},    \ ' Scope2kind ': {        \ ' ctype ': ' t ',        \ ' ntype ': ' N ' 
   \},    \ ' Ctagsbin '  : ' gotags ',    \ ' Ctagsargs ': '-sort-silent ' \}
Input: Tagbar can open Tagbar, the right side of the window will display a series of symbols. You can use CTRL +] to jump to the symbol definition, CTRL + T to return. The effect is as follows:



4. Grammar detection

The VIM plugin syntastic supports syntax checking for various languages, marking the location of errors and the cause of the error:



Input: Errors will open the Quickfix window listing all error lists by: bp,:bn switch between multiple errors. : Syntasitcinfo Displays the syntax checker information for the current file. This plugin can be easily configured to see the help documentation.

"Location List all Updatedlet G:syntastic_always_populate_loc_list = 1" Collect errors when use Multple Checkerslet g: Syntastic_aggregate_errors = 1

5. Code hints

The last is the big kill device. Vim writing code is criticized by people who do not like the IDE, can be based on semantic code hints. This function can be achieved with Gocode. Gocode is also the go write, easy to install:

Go get-u github.com/nsf/gocode
Then CD to Gocode/vim, execute./update.sh, the actual is to copy the Vim plugin to ~/.vim/. Then edit the go file, there will be code hints:



In this way, the go development environment is configured successfully, please enjoy it ^ ^

Build a go development environment based on VIM

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.