Features Tutorials

Refactorings in GoLand: Move refactoring

In this blog post, we will cover the Move refactoring option as we continue to explore the refactorings provided by the IDE.

If you want to check out the other articles from this series, you can find them all here (links will be updated as new articles are published):

– Move & Copy
Extract variable/constant
Extract function/method
Inline variable/constant
Change Signature
Extract Interface (new in 2019.1)

This is currently my second favorite refactoring, especially during the phase of the project that needs me to restructure how the code is organized.

With the Move refactoring, you can take any top-level named identifier from your source code and move it either to a separate file or to an entirely different package, existing or new.

In the example below, we can see how to move a function declaration to a new file.

Refactorings - Move

The Move refactoring does not only move the declarations, but will also add the required imports, update the references to the identifier, and perform a cleanup of the previous definition place of the identifier so that your code can still work.

Refactorings - Move 2

You can choose to export declarations when using this move, move related declarations, or move multiple declarations at once. And if you want to see which code paths are affected by this change, you can preview the locations that will be affected, with the option to atomically undo everything using the Undo shortcut.

Refactorings - Move 3

That’s it for today. We hope the Move refactoring will become an invaluable tool under your toolbelt if it isn’t already!

As we approach the end of this series on refactorings, we look forward to your feedback both on what we can talk about in future posts, as well as what features you’d like to see in the IDE.

General feedback about the IDE is very welcome, too, so please don’t hesitate to share your thoughts in the comments section below, on our issue tracker, or on Twitter.

image description