Features Tutorials

Refactorings in GoLand – Extract Interface

In today’s article, we will talk about the Extract Interface refactoring, a new refactoring available since GoLand 2019.1.

This refactoring allows us to take a type and extract all the method definitions that we need, then create a new interface from them. This operation is particularly useful when going from a single implementation to using multiple implementations, such as in the example below.

To invoke this, you can use the Refactor This shortcut, Ctrl+Alt+Shift+T on Windows/Linux or ^T on macOS.

Refactoring - Extract Interface - 1

The Extract Interface dialog allows us to select any method from the existing type and control where will the interface be extracted. As such, you can create the interface in the current file/package, by default, or in a new file/package or existing ones but different than the current ones. You can also control if the methods should be exported or not, in case you do not want for external packages to be able to implement the interface.

Refactoring - Extract Interface - 2

If you want to know what other refactorings the IDE can do, you can read some of our previous articles dedicated to them here:

Move & Copy
– Extract variable/constant
– Extract function/method
– Inline variable/constant
– Change Signature

That’s it for today. We learned how to use a new and powerful refactoring feature that will help us as the code base grows. Please let us know your feedback in the comments section below, on Twitter, or on our issue tracker.

image description