Setting Up Vim for Writing Golang
programming
Published: 2016-03-14

This content has been moved from Qiita.

When coding in Go using Vim, you can make coding easier with syntax highlighting and tools like gofmt and goimports. However, I found various articles on this topic to be complex, so I decided to compile my own simplified guide.

Installing Golang

OS X

$ brew install golang

CentOS

$ sudo yum install golang --enablerepo=epel

Ubuntu

$ sudo apt install golang

Creating a Directory for GOPATH

$ mkdir $HOME/go

Setting Environment Variables in .zshrc (for bash, use .bashrc)

  • Edit with vim ~/.zshrc
export GOPATH=$HOME/go
export GOROOT=$( go env GOROOT )
export PATH=$GOPATH/bin:$PATH
  • Reload .zshrc (for bash, use .bashrc)
$ source ~/.zshrc

Installing vim-go

Installing Plugin Manager for Vim

We will use vim-plug as the plugin manager.

$ curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
    https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim

Editing .vimrc

  • Edit with vim ~/.vimrc
call plug#begin('~/.vim/plugged')
Plug 'fatih/vim-go'
call plug#end()

Installing the Plugin

  • Install the plugin
vim +PlugInstall +q +q
vim +GoInstallBinaries +q +q 
  • Add settings to run goimports
$ vi ~/.vimrc

let g:go_fmt_command = "goimports"