Programming with Go in Vim

Mar 3, 2014
• paul

Update 2016-02-16: anyone reading this post should stop reading and instead use vim-go

Having decided to give Go a proper, well, go, it was time to get setup in Vim. The good news is the support is
really rather, well, good. This article aims to cover the main steps required to kick start your development.

Firstly some assumptions on setup: my system is Ubuntu Linux running
bash. Although all of the steps can be adapted for other platforms/shells, I
will not include details in this article.

Step 1. Ensure you have a recent Vim version

Must be a recent version of Vim (more a demand of the plugins we will use later)

Step 3. Ensure you know how to add Vim plugins

I would strongly recommend using pathogen.vim to manage plugins. pathogen.vim"makes it super easy to install
plugins and runtime files in their own private directories." Additionally I manage my setup via
git submodules. All of the plugins we install later have instructions on how to
install via pathogen.vim.

Step 4. Install Go tools

These tools will help with omnicomplete (code completion), syntax checking, 'go to definition' support and
ctags for Go:

Once installed, generate help tags by opening Vim and running the command :Helptags (note capitalisation).

Step 6. Optionally configure these plugins

I will not explicity list my configuration options but instead refer the interested reader to my
.vimrc

Try it out!

If all has gone to plan, the following run through should work. First create a basic test:

$ mkdir -p ~/gostuff/src/github.com/myitcv/vimtest &&cd$_

For this test we will use an external libray to demonstrate jumping to a definition. Let's install that library now:

$ go get github.com/lib/pq

Create a simple file on which we can try out Vim's new Go capabilities:

// main.gopackagemainimport("fmt")funcmain(){fmt.Println("This is a test")}

Syntax highlighting

The first thing you should notice editing the file is syntax highlighting. Indeed running the command :set ft you
should see the result filetype=go

Omnicompletion

Now let's try omnicompletion. Start adding a new line as follows (the underscore represents the cursor position):

// main.gopackagemainimport("fmt")funcmain(){fmt.Println("This is a test")fmt.print_}

In insert-mode type <Ctrl-x><Ctrl-o> which is translated as: hold down Ctrl then hit x and then o. You should be presented
with a 'menu' of options:

See :help i_CTRL-X_CTRL-O for more details.

'Go to definition'

Ensure your example code matches the following, with the cursor as shown after the t of Println:

// main.gopackagemainimport("fmt")funcmain(){fmt.Print_ln("This is a test")}

In command-mode, type gd. This should cause a new split to open:

Tagbar

Close the split so that you are left with just main.go open. In command-mode type :TagbarOpen<CR> (the <CR> indicates
to hit 'return'). You should see tagbar outline appear on the right hand side:

Syntax checking

Now let's deliberately introduce an error into our source file:

// main.gopackagemainimport("fmt")funcmain(){fmt.Println("This is a test")a=b}

Save main.go and observe the vim-airline status bar updates to show a syntax error:

Furthermore, in command-mode if you type :Errors<CR> a quick fix window will open listing the errors.

Conclusion

And that's the basics done!

Support for Go in Vim is made pretty good with the addition of a few simple plugins and Go tools. There are a huge
number of options that can be tweaked to customise the various plugins we have installed (again, checkout my
.vimrc for some ideas) and keyboard shortcuts setup to cut down on
typing. But even this basic start point will have you up and running and go-ing nuts.