Thursday, July 21, 2016

Updating VIM for Go (Golang) Development

Since my last post, setting up Vim for Go development is not only easier but also makes developing with VIM much more powerful.  Just a single plugin, vim-go, is all that is needed for the Go-specific stuff and a host of new tools is now available to handle things like refactoring, linting, error checking, and more.

Go Tools

Make sure that you have Go installed and then get the various Go tools.


Go provides a lot of use packages that can be imported but it doesn't like it when you import a package and not use it.  Goimports will automatically insert the right imports for you by looking at your code and will remove unused imports from your source.  It's a great time saver!

go get

Godef lets you jump to the location where a symbol is defined.

go get -v
go install -v

Golint will lint your source and warns you of potential issues.
go get

Gorename helps to refactor Go code.

go get

Errcheck checks your code for actual errors that isn't just lint issues.

go get

Gocode provides autocomplete of your Go code.  When combined with vim-go and YouCompleteMe plugins, it allows autocomplete to appear as you're typing.

go get -u (-u flag for "update")
It's a bit different for Windows so follow the instruction from the link for more details.


Gotags generate tags for Go code.  Combined with Tagbar, it will provide a pretty display of the tags in your code.

go get -u

Guru is a tool that integrates with editors to help it understand Go code.

go get
go build

Oracle (Deprecated and replaced by Guru)

Oracle is a source analysis tool for Go program.

go get

Vim Plugins

It used to be that you would manually install a vim plugin for each of the various Go tools as well as using the the plugins that comes with Go itself.  Now, everything has been consolidated into a single Vim plugin, vim-go, and that's all you really need when it comes to Go-specific plugins.  A few other plug-ins such as Tagbar and YouCompleteMe are useful to complement your development environment, though.  I highly recommend that you use Vundle to manage your plugins.


Vim-go brings together all the various plug-ins and feature for Go development in VIM including autocomplete, snippet support, improved syntax highlighting, go toolchain commands, etc. in a single package.


YCM is a fast code completion engine for VIM that works as you type.  YCM requires VIM 7.3.584 or above and CMake (you'll need to compile the extension after it's downloaded).


A great way to view the tags in your code.  This requires that you have Exuberant Ctags and gotags installed.


My favorite plugin for navigating between VIM buffers.


" Go Specific Stuff
au BufRead,BufNewFile *.go set filetype=go                                     
autocmd FileType go setlocal softtabstop=4
autocmd FileType go setlocal shiftwidth=4
autocmd FileType go setlocal tabstop=4

" go-def settings
let g:godef_split=2
let g:godef_same_file_in_same_window=1

" go-vim settings
let g:go_fmt_command = "goimports"
let g:go_highlight_functions = 1
let g:go_highlight_methods = 1
let g:go_highlight_structs = 1

" tagbar settings                                                                  
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' : {
        \ 't' : 'ctype',
        \ 'n' : 'ntype'
    \ },
    \ 'scope2kind' : {
        \ 'ctype' : 't',
        \ 'ntype' : 'n'
    \ },
    \ 'ctagsbin'  : 'gotags',
    \ 'ctagsargs' : '-sort -silent'
\ }