I’ve recently started playing with Go for some TCP code I’m working on. Once Hello World was running, the next step was to try and get IntelliJ IDEA working. While it was pretty straightforward, there are a few things worth mentioning.
Disclaimer: I’m completely new to Go, so if you spot any mistakes, please let me know.
First and foremost you need to install Go. The easiest way I found was to just download the installer package from the WebSite and click my way through it. I’m using version 1.5, after unsuccessfully messing around with 1.3 installed via brew.
Before configuring IntelliJ IDEA, it’s good to make sure Go is properly working. In other words, basically check that hello.go works.
The instructions on the Go site are pretty straightforward.
A word on workspaces and GOPATH
Go likes to organize things under a work directory which has the following tree structure
The GOPATH environment variable points to the root folder of where you want this structure. In my case this was
Apparently, and someone please correct me if I’m wrong, you should kind of keep within this structure with Go, i.e. don’t go placing your projects in different folders all over your hard-drive.
Also, for things to work correctly, make sure that the GOPATH is on your system path. The installer creates a symbolic link to the Go executable under /usr/local/bin/go.
Learn more about workspaces.
Setting up IntelliJ IDEA
There is an open source Go plugin for IntelliJ IDEA, and in the last year or so, two of my colleagues, in particular Alexander Zolotov and Sergey Ignatov are proactively contributing to it.
The instructions to install the plugin on the project page are pretty clear. Simply add a new repository (choose between nightly or alpha) and you can then install it from inside IntelliJ IDEA.
Creating a new project
The plugin provides an entry in the Project Wizard to create projects and additionally select frameworks and libraries
Once you have the project created, you can simple add Go Files and Run/Debug them straight from IntelliJ IDEA
If opening an existing project, IntelliJ IDEA should automatically detect it as a Go project and prompt you for any further action if required, i.e. just follow the instructions. It’s simple!
The most recent builds of the plugin provide support for debugging out of the box using Delve, so there’s no more need to set up GDB and go through the entire process of creating code certificates to sign it, etc. (which also works but quite tedious).
Important to note though that you need version 1.5+ of Go for it to work. Also, the plugin is not fully compatible with IntelliJ IDEA 15, which is currently in EAP. While many of the features do work, debugging does not. So if you’re missing the context menu to debug your app, that’s why.
Build and Make Project
Build and Make Project have no effect in Go, even though the options are available. In Go, you pretty much need to run the code using the context menu or hotkeys (Shift+Ctrl+R for Run and Shift+Ctrl+D for Debug).
IntelliJ IDEA does however provide error highlighting which makes it extremely useful, especially when you’re new to the language.
Installing Go apps
go install hello.go
compiles the source file and places the binary in the bin folder under GOPATH, which then allows you to simply call the app. Currently, unless you run this from the terminal tool window inside IntelliJ IDEA or configure it as an external tool, there’s no corresponding action to do this for you. And I’m not really sure it’s required as such either.
Formatting code on the fly
IntelliJ IDEA provides code formatting and you can of course invoke it at any time using Alt+Cmd+L (on OSX 10.5+ keybinding) . However, if you want to use GoFmt and have IntelliJ IDEA automatically format your code on save, take a look at Jame’s post on how to accomplish this, as well as use GoImports with File Watchers.