Using extract and inline refactorings
With extract and inline refactorings, you can quickly clean up your code and make it more streamlined or explicit.
Select and right-click the following code fragment:
&User{ ID: 1, Name: "Smith",}
.Navigate to Refactor | Extract | Variable.
Type u as a name for a new variable.
![Extract a variable Extract a variable](https://resources.jetbrains.com/help/img/idea/2024.3/go_tutorial_extract_inline_variable.png)
Code snippet:
package main
{...}
Select and right-click the following code fragment:
&User{ ID: 1, Name: "Smith",}
.Navigate to Refactor | Extract | Method.
![Extract methods Extract methods](https://resources.jetbrains.com/help/img/idea/2024.3/go_extract_method.png)
Code snippet:
package main
{...}
In the editor, select an expression or declaration of a variable you want to replace with a constant.
Press CtrlAlt0C to introduce a constant or select Refactor | Extract | Constant in the main menu.
note
By default, GoLand uses the in-place refactoring. To use the dialog for the refactoring, open the Settings dialog (CtrlAlt0S) , go to Editor | Code Editing, and select the In modal dialogs refactoring option in the Refactorings area.
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.
In the editor, select an expression or its part that you want to extract. You can also place the caret within the expression, in this case GoLand offers you a list of potential code selections.
Press CtrlAlt0M or go to Refactor | Extract | Extract Method in the main menu.
Type a method name and press Enter.
Thanks for your feedback!