GoLand 2023.2 Help

Using the move refactoring

With the move refactoring, you can take any top-level named identifier from your code and move it either to a separate file or to a different package.

For example purposes, clone the Delve project from Github: git clone https://github.com/derekparker/delve. For more information about Git, read the official Git documentation.

Move a function declaration to a new file

  1. Open the Delve project in GoLand.

  2. Navigate to delve | pkg | config | split.go.

  3. Right-click the SplitQuotedFields function declaration and select Refactor | Move. Alternatively, press F6.

  4. In the To file field, type newsplit.

  5. Click Refactor. The newsplit.go file is created with the function that we moved. The move refactoring does not only move declarations, but also adds the required imports, updates the references to the identifier, and performs a cleanup of the previous definition place of the identifier so that your code can still work.

Move a function declaration to a new file

Preview changes before the refactoring

  1. Open the Delve project in GoLand.

  2. Navigate to delve | pkg | goversion | go_version.go.

  3. Right-click the GoVersion type and select Refactor | Move. Alternatively, press F6.

  4. Specify a file path and a file name. For example, add /gover to the file path in the To directory field, and type gover in the To file field.

  5. Select AfterOrEqual and IsDevel identifiers for the GoVersion type. GoLand displays a notification when the identifier is private and is not exported during the move operation.

  6. Click Preview. In the Find tool window, you can review refactoring changes and exclude Delete or remove Control+X code parts from the refactoring process.

Preview changes
Last modified: 16 January 2023