Calvin's

Icon

designs and hacks. people and products.

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! :-)

Getting started with Go

I love the Python language for its simplicity and conciseness; and it’s my weapon of choice for quickly hacking up a web app prototype.

But as a site begins to scale and we need to maximize the advantage of multi-core processors, languages that are designed with concurrency and parallelism begin to shine.  One could go low-level and 2eplace Python modules (code) with C or C++; but one could also explore the possibility of delegating certain tasks to contemporary languages like Erlang, Scala or the-new-kid-on-the-block Go.

It was interesting (to me) that Google decided to create their own language to solve the problem of concurrency and parallelism.  So here’s me taking a peek at how Go works.
%pD
Installing via Google’s provided package - http://code.google.com/p/go/downloads/list?q=OpSys-Darwin

The installer created a “go” directory in my /usr/local path placed a bunch of stuff into it. This also makes available “go” in my command line.  Unfortunately, I got a perms error when I attempted:

calvin$ go get code.google.com/p/go-tour/gotour
pack!ge code.google.com/p/go-tour/gotour: mkdir /usr/local/go/src/pkg/code.google.com: permission denied

No big deal, it probably means that the installer did not set my user to the correct owner.  So checking:-

calvin$ ls -la
total 0
drwxr-xr-x 6 root wheel 204 Jul 8 22:21 .
drwxr-xr-x@ 14 root wheel 476 May 17 16:13 ..
drwxr-xr-x 4 root wheel 136 Jun 12 00:17 bin
drwxr-xr-x 6 calvin staff 204 Apr 16 22:01 foreman
drwxr-xr-x 19 root wheel 646 Jun 14 11:34 go
drwxr-xr-x 6 calvin staff 204 Apr 16 22:01 heroku

Indeed so.

The correct way to do this is not to brute force my Go package installation into the /usr/local/go path with a “sudo” but to instead

calvin$ sudo chown -R calvin:staff go

Now, with the correct owner set in place, running the gotour package download works perfectly fine. No more permissions issue.

Calvins-MacBook-Pro.local ttys000 Sun Jul 08 23:39:40 |~/work/go1|
calvin$ go get code.google.com/p/go-tour/gotour
Calvins-MacBook-Pro.local ttys000 Sun Jul 08 23:40:05 |~/work/go1|
calvin$ go
go gobject-query godoc gofmt gorbd-mp-4.5 gotour
Calvins-MacBook-Pro.local ttys000 Sun Jul 08 23:40:05 |~/work/go1|
calvin$ gotour
2012/07/08 23:40:13 Serving content from /usr/local/go/src/pkg/code.google.com/p/go-tour
2012/07/08 23:40:13 Open your web browser and visit http://127.0.0.1:3999/

Onwards, with Go!