Using extract and inline refactorings
With extract and inline refactorings, you can quickly clean up your code and make it more streamlined or explicit.
Extract a variable
Select and right-click the following code fragment:
&User{ ID: 1, Name: "Smith",}
.Navigate to
.Type u as a name for a new variable.
Code snippet
Extract methods
Select and right-click the following code fragment:
&User{ ID: 1, Name: "Smith",}
.Navigate to
.
Code snippet
Extract a constant
In the editor, select an expression or declaration of a variable you want to replace with a constant.
-
Press Ctrl+Alt+C to introduce a constant or select
.Select an expression you want to extract as constant and press Enter. Select the number of occurrences you want to replace and a name you want to use.
Code snippet
Extract a function or a method
The Extract Method refactoring lets you take a code fragment that can be grouped together, move it into a separated method and replace the old code with 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.
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.
Press Ctrl+Alt+M or from the main menu, select
.Type a method name and press Enter.