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

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.

About Florin Pățan

Developer Advocate at JetBrains
This entry was posted in EAP, New Features and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *