Development Environment for Go Lang

Being new to golang but generally familiar with how things work in Python and Ruby and using vim as my primary editor, my first questions when starting out are

  • How do I get the golang syntax highlighting, autocompletion and the usual cool stuff I have with Python/Ruby in my vim editor?
  • Are there project isolation tools, like Python’s virtualenv or Ruby’s rvm, in the golang world?
  • Where are the good places to look for and download golang libraries – i.e. the equivalent of Python’s pypi and Ruby’s rubygem?

golang on vim

In a previous post, I talked about the brain-dead installation of go set-up packages from google.  If we check out what sits inside the packages installed in /usr/local/go:


Calvins-MacBook-Pro.local ttys003 Sat Jul 14 11:14:25 |~|
calvin$ tree -d -L 2 /usr/local/go
/usr/local/go
├── api
├── bin
├── doc
│   ├── articles
│   ├── codewalk
│   ├── devel
│   ├── gopher
│   ├── play
│   ├── progs
│   └── talks
├── include
│   └── plan9
├── lib
│   ├── godoc
│   └── time
├── misc
│   ├── IntelliJIDEA
│   ├── arm
│   ├── bash
│   ├── bbedit
│   ├── cgo
│   ├── chrome
│   ├── dashboard
│   ├── dist
│   ├── emacs
│   ├── fraise
│   ├── goplay
│   ├── kate
│   ├── notepadplus
│   ├── osx
│   ├── swig
│   ├── vim
│   ├── xcode
│   └── zsh
├── pkg
│   ├── darwin_amd64
│   ├── obj
│   └── tool
├── src
│   ├── cmd
│   ├── lib9
│   ├── libbio
│   ├── libmach
│   └── pkg
└── test

We can see that it contains a very nice tree hierarchy.

Of particular interest is the directory misc/vim for our vim editing purposes.

If you are using vim-pathogen, as I am, all your have to do is to grab everything in /usr/local/go/misc/vim, push it to a remote git repository as a “vim-golang” repository and then run git submodule add your_git_remote_repo bundle/vim-golang in your .vim directory.

Or, you can totally skip all that and simply grab from https://github.com/jnwhiteh/vim-golang.

cd ~/.vim
git submodule add git://github.com/jnwhiteh/vim-golang.git bundle/vim-golang

and done.

GOPATH

golang is a statically-typed language.  This means that unlike Ruby or Python, we compile golang into a binary before using it.  What this really means is that the idea of isolated project execution at runtime does not make sense.

However, we can still group our development-time packages into specific directories.  However, there are no existing tools equivalent to virtualenv or rvm for that yet as golang is relatively new.

What we can do at this moment however, is to use GOPATH in our .bashrc or .bash_profile (my preference) to define the GOPATH environment variable, like this:

export GOPATH="$HOME/gocode";

where $HOME/gocode is the directory which `go get` commands will now download packages into.

Here’s an example:

go get github.com/hoisie/web

which will be resolved by go get, somewhat the equivalent of pip in python, to grab web.go web framework library and install it into our $HOME/gocode location.

This implies that if we want to segregate our golang packages when starting on a different golang project, what we can do is to manually change the location of GOPATH in our .bash_profile.  A little clunky, but perhaps I or someone else in the community will write a more convenient command line tool to do something like `workon myproject1` ala virtualenvwrapper in python.

With these done at the moment, however, it looks like we have gotten our basic development environment all set up for golang experiments and it’s time to mess around with some code! :-)