Intro

Golang is pretty hot right now. Over the past few weeks, I've been exploring implementing some of the cloud infrastracture I'd previously built with node in go, partly for fun, partly because go is fast. This led me to believe that setting up a basic REST service would make for a great tutorial.

At the end, we will have a fully functioning REST service for serving basic CRUD on a user resource. Code for the final product can be found here.

This is the first in a series of high level tutorials on setting up a REST service using golang, focused on getting a go dev environment setup.

Setup

The first step to start rocking some go is to get it installed. Being a mac user, I initially installed go globally with brew, but then reverted to using gvm, as I love the flexibility I get with its node counterpart, nvm.

So now we have a local version of go installed, our GOPATH and PATH are setup, and we have access to the go executable. Now what? One of the neat things about gvm is the notion of pkgsets, basically allowing you to define separate "workspaces" and group a set of go projects using the same go version.

Let's create a pkgset called "work".

# Create the pkgset
$ gvm pkgset create work
# Set it as active, and the default choice
$ gvm pkgset use work --default

Sweet. Now, if we inspect our GOPATH and PATH, we can see that they have been updated to reflect our pkgset location. But what if we don't want to work in ~/.gvm/pkgsets/go1.4/work/? Let's setup a directory for our code, say ~/src/go.

# Or something else
$ mkdir -p ~/src/go/{bin,pkg,src}

Now that we have the proper folder structure for go, we'll need to update our environment. We could manually set GOPATH and PATH to be prefixed with our new src directory, but there's a better way. We'll use the gvm pkgenv command with our fresh work pkgset so that our new workspace will always be found in GOPATH.

Enter the following to pull open the environment variables for the work pkgset. We'll be changing the GOPATH and PATH variables to look at ~/src/go. Obviously, adjust your entries to reflect the directory structure on your machine.

Fin

That's the end of the introductory tutorial on building a REST service with golang. We've setup a proper go dev environment and have built a simple go program. Next we'll learn about using third party packages and setting up a basic webserver. Check it out.