Intention actions
As you work in the editor, GoLand analyzes your code, searches for ways to optimize it, and detects potential and actual problems. Intention actions cover a wide range of situations from errors and warnings to optimization suggestions.
In GoLand, you can use the following types of intention actions:
Intention actions : an action that generates a piece of code or suggests an alternative variant for your code. Usually, an intention action is applied to a valid piece of code. By clicking the yellow bulb icon, you can view intention actions available in the current context. For example, you can create a constructor that generates values of a struct type. Or, replace the if-else statement with the equivalent one but with a negated condition and swapped if-else branches.
Quick-fixes : an action that suggests a solution for a problem in your code. Usually, the intention action is applied to a code that has an error. For example, when you missed brackets at the end of the expression. Or, when you forgot to convert a value to a specific type.
Apply intention actions
Place the caret at the code element that you want to modify. Then click the light bulb icon (or press Alt+Enter) to open the list of suggestions.
Intention preview opens automatically. If an intention is complex, and the preview cannot be generated, you will see the intention description. Hover the mouse pointer over available intentions on the suggestion list to preview them.
Press Enter or click an intention on the list to apply it.
You can toggle the preview off and on by pressing Control+Q or by enabling Show preview for intention actions when available in Settings (Control+Alt+S) .
Assign a shortcut to an intention
If you want to apply intention actions quicker, you can assign shortcuts to them. Currently, you cannot assign shortcuts to fixes that belong to code inspections.
In the editor, place the caret at the code that triggers the intention action for which you want to assign a shortcut, and click the bulb icon or press Alt+Enter.
A list of available suggestions opens.
In the list, click next to the necessary intention and from the menu that opens, select Assign shortcut….
In the dialog that opens, specify the shortcut that you want to use for this action and click OK.
After that you won't need to open the list of suggestions to apply this intention action in the editor; you will be able to do it right away by pressing the configured shortcut.
You can find all your custom intention action shortcuts in settings: press Control+Alt+S, click Keymap, and locate the Intentions category.
View all intention actions
You can view the full list of available intention actions, and disable actions that you don't need at the moment. To open the list of intention actions:
Press Control+Alt+S to open the IDE settings and then select
.For each intention action, you can see a description and an example. To disable an intention, clear the checkbox next to it.
Apply the changes and close the dialog.
Disable intention actions on the fly
Place the caret at the code element that you want to modify. Then click the light bulb icon (or press Alt+Enter) to open the list of suggestions.
Select the action you want to disable, click the right arrow next to it, and then click Disable <intention action name>.
Disable the intention action bulb icon
You can disable the intention action icon and the quick-fix icon in the editor.
Press Control+Alt+S to open the IDE settings and then select
.Disable the Show intention bulb option, apply the changes, and close the dialog.
Examples of intention actions
Add a format string argument
The Add format string argument intention action generates a placeholder for the expression. The intention action inserts the placeholder into a format string and passes the expression as an argument for the placeholder.
Intention actions for declarations
You can perform the following actions with Go declarations by using intention actions:
Add parentheses to declaration
Remove parentheses from declaration
Merge declaration up
Merge declaration up via comma
Merge all declarations
Split declarations into two groups
Split declaration by comma
Split all declarations
Fill struct fields
When you create a new struct, you can automatically generate field names for it.
Place a caret between braces of a struct.
Press Alt+Enter.
Select Fill all fields.
Enter field values.
Examples of quick fixes
Convert to the expected type
The Convert to the expected type quick-fix Alt+Enter converts a value to the expected type.
Finish a call expression
The Finish call expression quick-fix adds missing brackets and completes the expression.