Add GoLang syntax highlighting for VIM

UPDATE:

Go 1.4 Release Notes

Miscellany

The standard repository's top-level misc directory used to contain Go support for editors and IDEs: plugins, initialization scripts and so on. Maintaining these was becoming time-consuming and needed external help because many of the editors listed were not used by members of the core team. It also required us to make decisions about which plugin was best for a given editor, even for editors we do not use. The Go community at large is much better suited to managing this information. In Go 1.4, therefore, this support has been removed from the repository. Instead, there is a curated, informative list of what's available on a wiki page.


The standard Go distribution includes Go files for Vim in go/misc/vim/. This directory contains a readme.txt file which contains installation instructions.

readme.txt

Vim plugins for Go (http://golang.org)

To use all the Vim plugins, add these lines to your $HOME/.vimrc.

" Some Linux distributions set filetype in /etc/vimrc.
" Clear filetype flags before changing runtimepath to force Vim to reload them.
filetype off
filetype plugin indent off
set runtimepath+=$GOROOT/misc/vim
filetype plugin indent on
syntax on

If you want to select fewer plugins, use the instructions in the rest of this file.

<<..SNIP..>>


On Debian, I suppose it's the same on ubuntu, you just :

sudo apt-get install vim-gocomplete gocode vim-syntax-go
vim-addon-manager install go-syntax
vim-addon-manager install gocode

For the best syntax highlighting try https://github.com/fatih/vim-go

It's a new project that consolidates many vim plugins and adds a lot of features. From the readme:

  • Improved Syntax highlighting, such as Functions, Operators, Methods..
  • Auto completion support via gocode
  • Better gofmt on save, keeps cursor position and doesn't break your undo history
  • Go to symbol/declaration with godef
  • Automatically import packages via goimports
  • Compile and go build your package, install it with go install
  • go run quickly your current file/files
  • Run go test and see any errors in quickfix window
  • Lint your code with golint
  • Run your code trough go vet to catch static errors.
  • Advanced source analysis tool with oracle
  • List all source files and dependencies
  • Checking with errcheck for unchecked errors.
  • Integrated and improved snippets. Supports ultisnips or neosnippet
  • Share your current code to play.golang.org

you can just add these lines to your ~/.vimrc:

set rtp+=$GOROOT/misc/vim
filetype plugin indent on
syntax on

EDIT This assumes filetype plugin indent off before these lines (i.e. beginning of .vimrc file) and may cause problems if it's not. See @peterSO's answer below for the safer version.