Setting up a Go IDE

I want to learn Go, and to do that I want to have a nice IDE that allows me to execute a build, run some tests and see what the errors are, all from within the IDE.

Doesn’t sound like much of an ask though, does it? Well there’s a bit of a learning curve out there with regard to setting up an Go environment. I’ve managed to pull something together that suits me, and maybe it’ll suit you too. First off, there are a few options* that you can follow:

1) Use VIM — the following gives you some idea on how to setup VIM for Go. I didn’t follow this approach as I’m not as great with VIM as I could be, and I don’t want to have the frustrations of VIM put me off from learning Go.

2) Use ATOM — ATOM is an IDE from Github, and go-plus provides an improved Go experience for the Atom Editor. This looks like a nice extension, but I want to execute my app as well as having the nice features that go-plus gives you, such as autocomplete, formatting, code quality checks etc. Atom & Go Plus may very well work for you, and if it does please add some tips to the comments section of this doc — sharing is good.

3) Use Sublime Text — This is the approach I have followed, principally because I use Sublime a lot and have a license, but also because I can get the nice formatting and validation features combined with the ability to execute my app from within the IDE as well. Below is what I’ve ended up with:

To get to this point, I followed the following steps:

  1. Install Go — I downloaded the Go Tools from the golang site
  2. Set your GOPATH and GOROOT. Given that I am on OSX, I set it from the terminal by adding the following to the ~/.bash_profile
  3. 1. export GOPATH=/Users/davidmcg/Dropbox/Github/gocode
  4. 2. export GOROOT=/usr/local/go
  5. Add $GOPATH/bin:$GOROOT/bin to your PATH variable — again I added this to ~/.bash_profile
  6. Install Sublime Text — you can easily get Sublime Text from here
  7. Install the package control for Sublime Text by following these steps
  8. Once you have package control installed, you’ll want to install 2 packages:
  • GoSublime — Provides code completion and other IDE-like features such as syntax checking as you type, code formatting and removing unused imports.
  • Sublime-build — The official Sublime Text package for Go build system integration. Golang Build is a Sublime Text package for compiling Go projects. It provides integration between Sublime Text and the command line go tool.

9. To install these packages, run the following bring up the command palette and start typing Package Control: Install Package then press return or click on that option to activate it.

You will be presented with a new Quick Panel with the list of available packages. Type GoSublime or sublime-build and press return to install each package individually.

Configuring GoSublime

To configure GoSublime, you need to configure Sublime Text to run a number of commands on the save of the .go file. The commands will check the code quality, and apply formatting. First, you need to access the settings file for your user, within Sublime Text:

Once you access this section, you’ll need to paste in the following. Note — you’ll need to change your path to match your own system. [CREDIT– I got this from Jonathan Gautheron’s excellent medium post ]

{
 “env”: {
 “GOPATH”: “YOUR GO PATH GOES HERE”,
“PATH”: YOUR PATH GOES HERE”
 },
 “fmt_cmd”: [“goimports”],
 “on_save”: [{
 “cmd”: “gs9o_open”, “args”: {
 “run”: [
 “sh”,
 “go build . errors && go test -i && go test && go vet && golint .”
 ],
 “focus_view”: false
 }
 }],
 “autocomplete_closures”: true,
 “complete_builtins”: true
}

Additionally, you will need to install the golint and goimports tools. Golint prints out style mistakes, and goimports removes unneeded import statements from your code. To install these tools, type the following in a terminal:

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

Configuring Sublime-build

So, we have source formatting occurring, but the next step is to be able to actually run our app from within Sublime Text. Thats where sublime build comes in. You should have installed the sublime build package in step 4.2 above. Once you’ve done that, you’ll need to select it as the build system within Sublime Text, as follows:

And once you have done that, then set the ‘Tools | Build With’ value to be ‘Go Run’ And thats you setup.

Once you have done these steps, you can hit the following commands:

  • ⌘ S (Save) — will run the GoSublime on save commands and format your code, check syntax and make sure the imports are correct.
  • ⌘ B (Build) — will run your app for you, with output in the Sublime console window. Note, you can set this value to be go run, go test, go install from the Build With setting. Each time you press ⌘B, it will execute that command.

One final thing — as per Jonathan Gautheron’s medium post, I have also set Sublime Text to use the excellent Predawn theme. The following providesinstructions to set this up for yourself.

Please feel free to point out anything that I may have missed/could have done better. Good luck and happy learning!

* There are other Go Editors out there — the following provides some more details on these other editors. I just wanted to use Sublime Text 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *