GoLand 2023.2 Help

Extract method

With the Extract Method refactoring, you can take a code fragment, move it into a separated method and replace the old code with a call to the method. If the code always exits in return statements (all execution paths lead to possibly implicit return statements), it is extracted as is. However, if it contains execution paths without returns, the extracted method will have an additional bool flag as a return value. The flag is used to perform an early return on the call side.

When you extract a function by using the Extract Method refactoring, GoLand keeps the original order of parameters of a parent function.

Extract functions and methods

  1. In the editor, select an expression or its part that you want to extract. You can also position the caret within the expression, in this case GoLand offers you a list of potential code selections.

  2. Press Control+Alt+M or from the main menu, select Refactor | Extract | Extract Method.

  3. Type a method name and press Enter.

Example of the Extract a method refactoring

Code snippet at GitHub

Last modified: 23 August 2023