Sublime Text and Go

There are many editors out there, but only a handful of them really integrate well with Go.

Sublime Text is one of them.


Before I go through the few steps required to set up a working ST environment with Go, note that you can actually run a preconfigured Docker container.

docker run -it \
-v $HOME/workspace:/home/subl/workspace \
-v /tmp/.X11-unix:/tmp/.X11-unix \
-e DISPLAY=$DISPLAY \
jgautheron/sublime-text-go:latest

This container might prove useful if you‘d like to try the editor or the Go language without all the setup required.

You can also fork this project, roll your own, commit your changes. Wherever you will be, you can always run your editor, how cool is that?!


Install Go

OK so you’d like to install it manually. Let’s proceed.

First, let’s install Go.

# OSX
$ brew install go
# Ubuntu/ Debian
# We don't bother with the package manager, it's always outdated
$ wget -qO - https://godeb.s3.amazonaws.com/godeb-amd64.tar.gz | tar zxvf - -C /tmp && /tmp/godeb install 1.5 && rm -f /go*.deb

If you’d like to be able to switch easily from one version to another, gvm will play well.

Then you have to define your $GOPATH. This is the directory that will contain all third party libraries, including yours. I like to set it to $HOME/go.

Edit your local .bashrc (Linux/Bash), .bash_profile (OSX), or .zshrc and add the following:

# Go environment
export GOPATH="$HOME/go"
# Expose the Go binaries in the PATH
export PATH="$GOPATH/bin:$PATH"

Then reload your file with source and you are all set! now it’s time to…

Install and configure Sublime Text

You can get it here.

Once installed, you will need to install the second most important thing: Package Control. A sort of package manager for Sublime Text, this is a must-have. The instructions are pretty clear, and so far for me, it always worked. Open the console, copy/paste the snippet, press ENTER.

Restart ST.

The magic starts now. Open the command palette (ctrl+shift+p for Windows/Linux, cmd+shift+p for OSX), type in: “install”, it will automatically resolve the “Package Control: Install Package” command.

Then you can search for “GoSublime” and install it.

Go Development Workflow

I like to get instant feedback while I’m working so that I can address the issues raised right away. Linting, testing, static analysis…

It is common to run these tools in a CI build, this is a good thing to do, but waiting for a report from your CI system is most likely going to end up in procrastination!

So we’re going to configure a hook that will be ran at each Go file save.

Open Preferences > Package Settings > GoSublime > Settings - User, and copy/paste the following.

Notice the goimports set for the fmt_cmd? This part will handle the formatting of your file before it’s saved (using go fmt) and automatically add or remove imports for you.

You’re almost done! We are missing a few tools used above.

go get -u github.com/golang/lint/golint
go get -u golang.org/x/tools/cmd/goimports

Done! Now when you’ll save a Go file, the reports will be automatically fed into the ST console.

Tests OK, missing a few comments!

Was that long? it definitely takes some time to go through all these steps. That’s why the Docker container is so useful, it already has everything pulled and configured.


Theme

You might like the default theme but in the event you’d like to try another, here’s a short list of Sublime themes:

  1. Sublime Material — the one used in the container
  2. SpaceGray
  3. Predawn

Enjoy!