GoLand 2018.3 Help

Refactoring Source Code

Refactoring is a process of improving your source code without creating a new functionality. Refactoring helps you keep your code solid, dry, and easy to maintain.

Invoke refactoring

  1. Select an item to refactor. You can select a file/folder in the Project tool window or expression/symbol in the editor.

  2. Press Ctrl+Shift+Alt+T to open a pop-up list of refactorings that can be selected. Alternatively, you can use a keyboard shortcut for a specific refactoring.

If you need to undo your refactoring, press Ctrl+Z.

Preview refactoring changes

For some refactorings, GoLand lets you preview changes before applying them.

  1. To see potential changes, click Preview in the Refactoring Preview dialog.

  2. Check the changes that are going to be made in the Find tool window. You can Exclude (Delete) or Remove (Ctrl+X) changes that you consider unnecessary.

  3. Click Do Refactor (N/A) to proceed with the changes.

The Preview changes window

Resolve conflicts

When GoLand encounters problems with your refactoring, it opens a dialog with the list of conflicts and their brief descriptions.

  • Click Continue to ignore the problem and proceed to open the preview in the Find tool window.

  • Click Show Conflicts in View to open the conflict entries in the Find tool window and deal with them further.

    For example, you can try to exclude an entry from refactoring by pressing Delete or click Cancel and return to the editor.

Configure refactoring settings

  1. In the Settings/Preferences dialog (Ctrl+Alt+S), select Editor | General.

  2. On the General page, in the Refactorings section, adjust the refactoring options and click OK.

Rename refactoring

The Rename refactoring lets you change names of symbols, files and all the references to them throughout code.

Renaming local variables or private methods can be done easily in-line since only the limited scope is affected. Renaming types, interfaces, or public methods could potentially impact a lot of files. In this case, preview potential changes before you refactor.

Invoke Rename refactoring

  1. In the editor, select an element you want to rename. If you need to rename a file or a directory, select one in the Project tool window.

  2. Press Shift+F6 or from the main menu, select Refactor | Rename.

  3. You can perform a rename refactoring in-place or press Shift+F6 again to open the Rename dialog if you need to specify additional options. For example, specify where to search for element occurrences, or what else to rename.

  4. In the Refactoring Preview dialog, review potential changes and click Do Refactor.

The refactoring dialog

Change signature refactoring

The Change Signature refactoring combines several different modifications that can be applied to a function, method, or a method specification. You can use this refactoring for the following purposes:

  • To change the method name.

  • To add new parameters and remove the existing ones.

  • To assign default values to new non-variadic parameters.

  • To reorder parameters.

When changing a method signature, GoLand searches for all usages of the method and updates all the calls, implementations, and override replacements of the method that can be safely modified to reflect the change. When you refactor a method specification, you will see a notification dialog where you can select to update all implementations as well.

Change a function signature

  1. Place a caret on a function name that you want to refactor.

  2. Press Ctrl+F6. Alternatively, select Refactor | Change Signature from the main menu or from the context menu.

  3. In the Change Signature dialog, make the necessary changes. Consider the following list of actions that you can perform:

    • Change the function name. To change the name, edit the text in the Name field.

    • Manage the function parameters. To configure the parameters, use the table and the buttons in the Parameters area:

      • To add a new parameter, click the Add icon (The Add icon) and specify properties of the new parameter in the corresponding table row.

      • To remove a parameter, select any row and click the Remove icon (The Remove icon).

      • To reorder the parameters, use the Up (The Up icon) and Down (The down icon) icons.

      • To change the name or the default value of a parameter, make the necessary updates in the table of parameters (in the fields Name and Default value respectively).

  4. Click Refactor.

Add a return value

  1. Click the function name and press Ctrl+F6. Alternatively, select Refactor | Change Signature from the main menu or from the context menu.

  2. In the Change Signature dialog, click the Result parameters tab and click the Add icon (The Add icon).

  3. In the Type text field, specify the return value.

  4. Click Refactor.

Delete refactoring

GoLand lets you use the SafeDelete refactoring to remove files and symbols from the source code safely. Before GoLand deletes a file or a symbol, it searches for usages and if they are found, GoLand lets you check them to make necessary adjustments.

Invoke Safe Delete refactoring

  1. Select an item you want to delete.

  2. Select Refactor | Safe Delete (Alt+Delete).

  3. In the dialog that opens, select necessary options and click OK.

  4. If GoLand encountered potential problems, it displays the Usages Detected dialog. Proceed with the appropriate action.

Extract refactorings

Extract a constant

  1. In the editor, select an expression or declaration of a variable you want to replace with a constant.

  2. Press Ctrl+Alt+C to introduce a constant or select Refactor | Extract | Constant.

    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.

Example of the extract a constant refactoring

Code example: Extract a constant

package main import "fmt" func plus(a int, b int) int { return a + b } func plusPlus(a, b, c int) int { return a + b + c } func main() { res := plus(1, 2) fmt.Println("1+2+3·=", res) res = plusPlus(1, 2, 3) fmt.Println("1+2+3·=", res) }

Extract a variable

If you come across an expression that is hard to understand or it is duplicated in several places throughout you code, the Extract Variable refactoring can help you deal with those problems placing the result of such expression or its part into a separate variable that is less complex and easier to understand. Plus, it reduces the code duplication.

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

  2. Press Ctrl+Alt+V or on the main menu, select Refactor | Extract | Extract Variable.

  3. Select a name suggested in the pop-up or type your own and press Enter. If GoLand finds more than one occurrence, it lets you specify whether or not you want to extract this variable for all of them.

Example of the Extract a variable refactoring

Code example: Extract a variable

package main import "fmt" func plus(a int, b int) int { return a + b } func plusPlus(a, b, c int) int { return a + b + c } func main() { res := plus(1, 2) fmt.Println("1+2+3·=", res) res = plusPlus(1, 2, 3) fmt.Println("1+2+3·=", res) }

Extract a function or a method

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

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

  3. Type a method name and press Enter.

Example of the Extract a method refactoring

Inline refactoring

The Inline refactoring lets you reverse the extract refactoring for a method or a variable.

  1. Place a cursor within the code fragment you want to inline.

  2. Press Ctrl+Alt+N. Alternatively, click Refactor | Inline.

  3. In the Inline dialog, specify the inlining options.

  4. Preview and apply changes.

Move and copy refactorings

Perform copy refactoring

You can create a copy of a file, directory or a package in a different directory or a package.

  1. Select an identifier that you want to refactor (for example, a file in the Project tool window). Alternatively, place a caret anywhere in your code.

  2. Select Refactor | Copy (F5)

  3. In the Copy window, specify the name and location for your copy, and click OK.

Perform move refactoring

You can move source code sections to another package or a file. Also, you can move a file or a folder in the Project tool window.

  1. Select an identifier that you want to refactor.

  2. Select Refactor | Move (or press F6).

  3. In the Move window, you see a list of declarations that you can move along with the item. Select all the necessary declarations.

  4. Specify a path to the target directory, a file name, and a package name.

    Example of the Move refactoring

Move private identifiers

Sometimes when you move an identifier that is not exported (a private identifier), the identifier becomes inaccessible for the source package (from which we perform the move).

GoLand displays a notification when the identifier is private and is not exported during the move operation. Before you apply code changes, consider reviewing them by using the Preview button.

Move private identifiers
Last modified: 11 December 2018

See Also

Web Resources: