As a developer who writes Golang everyday, high productivity is of utmost importance. Intellij Goland IDE provides so many useful features to reach this goal: from viewing the current staged changes to running a debugging session. After working with it for a few years I believe that Goland is an essential part of my work, and I’m willing to pay for the annual license even if my company does not. More than just a text editor, Goland provides a bunch of helpful shortcuts to help us execute trivial tasks quickly. A few developers, however, are agnostic of these shortcuts, and use Goland like Notepad.

In this blog post, we will explore some of my most used Goland features. All the examples will be presented from a macbook keyboard’s POV, but the equivalent in Windows and Linux should be pretty close.

Find definition / Find usage

This is the most basic thing that I look for whenever I learn a new IDE. “Find definition” means finding the place where the implementation of a function/method/interface/struct takes place. “Find usage” is the opposite of “Find definition”: finding where a function/method/interface/struct is used within a code base. Both of these actions can be achieved by pressing CMD + B.

You may be asking why I include this, isn’t this obvious? Well it turns out that some peopl have no idea this feature exists, or overlook how useful these simple shortcuts can be.

We’ll use gochuck-cli to demonstrate an example. Assuming that we’ve already synchronized all dependencies to our local Go cache, to check the implementation of cobra.Command, which is invoked on line 26 in main.go, we can simply move the cursor to it and press CMD + B.

go-to-definition

Goland should take us to where the method is implemented.

go-to-definition-2

The reverse of “Find definition” is “Find usage”. Moving our cursor to method Execute under cobra package and then pressing CMD + B will do the job. If there is only 1 place it’s called then the IDE will auto navigate to that place.

find-usage

If we want Goland to show us the usage of a struct/method/function in a separate window, Option + F7 will help. The screenshot below is an example of searching for all usages of GetCategories, even in comments. On the right side we also get a preview of it.

find-usage-2

The same process of “Find definition” and “Find usage” can be applied to any other types.

Show method/function callers and hierarchy

What if we want to show all the places where Execute is called, along with the hierarchy of its caller? Simply move the cursor to where it’s defined, and press Control + Option + H, a separate window like the following will appear.

call-hierarchy

It even shows us the tests where the method is called, very handy right?

Extract method/function

Ever had a long function/method that you want to wrap pieces of it in a smaller, better named function/method? Fortunately Goland provides a very handy shortcut for that.

First, select the code block we want to extract as another function/method. extract-method-function-1

Then press Option + CMD + M. extract-method-function-2

Note that sometimes Goland will extract our function/method in a very weird way, so it’s always better to revise it, and have our tests back us up.

Implement interface / Extract interface

We create a new type, and want it to implement a certain interface, io.Writer for example. How do we do that instead of manually checking what methods io.Writer require, and typing that method signature for our type? Simply move the cursor to the type we want to implement the interface, then press Control + I and select the interface we want.

implement-interface

On the contrary, to extract an interface from a concrete type, move the cursor to the type definition, and then right click on it, then select RefactorExtract Interface.

Generate table-driven tests

Table-driven testing is a very effective method. Goland allows quickly generating table-driven tests by pressing CMD + Shift + T. We can choose what function/method we want to generate tests for by moving our cursor to that function/method, or we can choose to generate tests for all exported functions/methods in the current file, or the current package.

generate-tests

Moving back and forth

While navigating large code bases, it’s normal to open many files at once, jumping back and forth between definition and usage of functions/methods/types. It may be easy to get lost while mentally juggling different parts of the code base. Surprisingly Goland keeps the position of the cursor in memory, using a list-like data structure. It’s possible to go back to our cursor’s previous position by pressing CMD + [, and go forward to the next by pressing CMD + ].

Format current file

Did you know that Goland provide a very useful shortcut to run gofmt on the current opened file? Simply pressing Option + CMD + L will do the trick.

Goland provides excellent search capability. The search function can be invoked by pressing Shift twice. It’s possible to limit the search scope by type, file, symbol, etc.

search

To quickly use the search file name, we can press CMD + Shift + O.

To search a certain text in all files, we can press CMD + Shift + F.

Browse all shortcuts

We go through roughly 10 shortcuts/tricks in this article, which is few compared to Goland’s full power. In fact we don’t have to remember them all! We can bring up all available shortcuts/features by pressing CMD + Shift + A, and then even further filter this list in a full text search manner.

all-actions

A shortcut can be extremely useful, or not so applicable depending on each person’s preference and situation. As a developer it’s our responsibility to get familiar with the tools in our arsenal. Practice these tools, and our productivity will skyrocket!