Last night I decided to take a crack at getting my environment more appropriately setup for Go programming. Being a huge fan of both RubyMine and IDEA, and having recently downloaded the latest versions of both, I set out to install the latest and greatest version of Mihai Toader’s Google Go Lang IDEA Plugin https://github.com/mtoader/google-go-lang-idea-plugin.
Turns out that involves a few non-obvious steps. Most of this info I mined from the project wiki. Here I have laid it out in a (hopefully) easy-to-follow tutorial.
Before you Begin
To get started you will need:
- Version 1.2 of Go (I installed it via brew)
- IntelliJ IDEA 13 Community Edition downloaded from http://www.jetbrains.com/idea/download/ (build 133.193 atm)
GOPATH
Standard GOPATH configuration as described on golang.org is to have a single workspace with singular src, pkg and bin directories containing multiple projects.
IDEA’s Go plugin, however, subscribes to the multiple workspace theory (aka the one-workspace-per-project theory). This means if you want to use the IDEA Go plugin, you need multiple workspaces. Thus your GOPATH env variable should look like so:
GOPATH=$HOME/workspace/golang:$HOME/workspace/proj1:$HOME/workspace/proj2
Each entry in GOPATH is a full Go directory structure with src, bin and pkg directories. The first entry is special — that’s the one where all external packages are installed. The rest are your projects.
Because of this multi-workspace approach you’ll need some special sauce to get all your bins on the path. Set your PATH env variable like so:
PATH=$PATH:${GOPATH//://bin:}/bin
Compiling the plugin
First thing to note: You should not simply download the plugin from IDEA’s plugin repo. That version is old.
Instead, we’re going to grab the latest version of the plugin from github and compile it ourselves:
git checkout https://github.com/mtoader/google-go-lang-idea-plugin.git
Got the code? Great. Next step is to fire up IntelliJ IDEA 13 and open the project for compilation. File -> Open -> ~/workspace/google-go-lang-idea-plugin
Here’s where it gets tricky. At the top of the IDE you should see the following message: “Project SDK is not defined” and a link to “Setup SDK”. Like so:
Click on “Setup SDK”, followed by “Configure”. You’ll see the following screen (ignore the Go SDK in the screenshot. You shouldn’t actually see it till later):
Click the left most + sign to add an additional SDK. Select “IntelliJ Platform Plugin SDK” from the drop down list. Now navigate to your “IntelliJ IDEA 13 CE.app” location and select it (in my case the path is /Applications/IntelliJ IDEA 13 CE.app). Click “Choose”, followed by “OK” when prompted to select 1.6 as the SDK.
The window should now look like this (minus that Go SDK):
Click OK, then OK again when it asks you to “Select Project SDK”.
Now the plugin can find the necessary IDEA class references for compilation!
You can try clicking make project at this point to make sure it works (green down arrow with 1’s and 0’s up top). Finally, navigate to the Build menu and select Prepare Plugin Module ‘google-go-language’ For Deployment. This will drop a jar file (google-go-language.jar) at the top level of the google-go-lang-idea-plugin directory.
Installation and Configuration
Almost Done! Now, inside of IDEA, navigate to Preferences. From there select Plugins and click on “Install plugin from disk…”. Find the jar file you created (google-go-language.jar) and select it.
The plugin is installed! Now to add a Go SDK:
Open up your Project Structure from the File menu and select SDKs on the left. Click the top-most plus sign and select “Go Sdk”. Navigate to your Go installation directory and find the libexec directory (if you installed through brew this is most likely at /usr/local/Cellar/go/1.2/libexec/).
Now you have a Go SDK! Final step is to configure its Classpath and Sourcepath so it will recognize your package code:
Navigate back to your Project Structure and select SDKs on the left once again. Select the Go SDK and take a look at the Classpath and Sourcepath on the right. To each of these (yes, both Classpath and Sourcepath. Not sure why but that was the magic incantation), you’ll need to add the path to the src and pkg directories in the first workspace in your GOPATH. Click the lowermost plus button and add the following paths (from my own machine for the sake of example):
- /Users/pivotal/workspace/golang/src
- /Users/pivotal/workspace/golang/pkg/darwin_amd64
When you’re done it should look like:
Click apply, followed by OK.
Invalidate the Caches
As a final step go to File -> Invalidate Caches / Restart…
DONE
Now you’re ready to develop in Go… with pleasure!