Setting up Intellij IDEA for your Golang project

After two full months of testing Golang plugin in Intellij IDEA, I’m happy I can say it’s sufficient and there are no real limits in using it - no more Sublimes, Atoms, LiteIDEs. If you’re not a Vim user and want to try real IDE with Golang, “go” for this. Here’re the steps to make your live easier too.

Installing Go and Golang plugin

I assume you have the Golang downloaded. If not, please download it first here.

In Intellij Idea go to the plugin list and browse the repository for Go plugin. You can’t miss it. After you install it, restart your IDE.

In case you’re working on the existing (imported/opened) project, check whether you have proper SDK selected. Idea probably asked you about it first time you opened it via notification, but if it’s not set correctly, some things below wouldn’t work.

Integrating Go tools

There are couple of preconditions you should meet before we proceed - mostly to avoid scenarios that something is working in the terminal and not in IDEA and vice versa:

Now add $GOPATH into global libraries so it nicely resolves all your dependencies:

GoImports

GoImports is a tool that automatically manages your imports - removes unused, adds unimported in case it’s resolvable and also formats your source.

In case you want to only format your code, use GoFmt instead.

In terminal:
In Intellij Idea:

After every save, GoImports will get triggered and your code will be formated and altered as necessary.

We need to one more fix related to saving. The problem here is that if autosave was trigerred every second, your caret would move around randomly all the time when GoImports is run and you would go nuts very quickly.

*Files are being altered by external tool and IDE would move the caret around every second because of autosave -> external change -> reload cycle. Disabling autosave is mandatory to be able to work with the GoImports.

To fix it:

GoLint

GoLint is a linter tool that will make you write and document your code properly. The sooner you start with it, the nicer code you’ll produce.

In terminal:
In Intellij Idea:

Unfortunately there’s no way how to stop build if GoLint has any kind of objections. It’s always returning exit code 0 - see the GitHub issue.

Linter will now be triggered with every build in separate tab.

Go Vet

Vet is a tool that tries to find logical issues in your source - e.g. checking if your Sprintf() string references the same amount of variables as you provided afterwards.

In Intellij Idea:

In case go vet finds something, it will stop the build as it returns non-zero exit code.

If you already had some run/debug configurations created prior these settings, you’ll have to add “before launch” commands into those configurations too. All new configurations will be created with Go Vet automatically.

Vendor experiment

To get more information about vendor experiment in Go 1.5, visit the official post. Vendoring is enabled by default since Go 1.6.

Conclusion

Because of the way how we set things up, all of the tools are also working from within IDE terminal which is nice. With the doc integration that IDEA provides by default (CTRL+Q), you now have really neat environment for developing in Go.

Intellij IDEA is also the only IDE I was able to use debugger in, which is a huge plus when you’re trying to catch a bug. The only problem I found currently is that when you kill the app, it kills debugger but binary keeps still running - you have to manually kill the process of your app.

The plugin is still in heavy development and things might change in the future (vendoring is not fully supported, interface implementations are not being recognized), but it’s on the way.