GoLand 2024.1 Help

Auto import

If you use a type, an interface, a method, or a function that you haven't imported yet, the IDE shows you a tooltip prompting to add a missing import statement so that you don't have to add it manually. Press Alt+Enter to accept the suggestion.

If there's more than one possible source of import, pressing Alt+Enter will open the list of suggestions.

the auto-import tooltip

Automatically add import statements

You can configure the IDE to automatically add import statements if there are no options to choose from.

  1. Press Ctrl+Alt+S to open the IDE settings and then select Go | Imports.

  2. Select the Add unambiguous imports on the fly checkbox, and apply the changes.

    Add unambiguous imports on the fly checkbox

Disable import tooltips

When tooltips are disabled, unresolved references are underlined and marked with the red bulb icon the Quick-fix button.

Disable all tooltips

  • Hover over the inspection widget in the top-right corner of the editor, click More, and disable the Show Auto-Import Tooltip option.

    The Show Auto-Import Tooltip option toggles the auto-import feature. The setting does not influence the auto import functionality when you select constraints from the code completion list.

    Autoimport with popups disabled

Disable auto import

If you want to completely disable auto-import, make sure that:

Exclude packages from auto import

The list of import suggestions may include packages that you don't need. You can exclude redundant entries from automatic import so that the list of suggestions contains only relevant items.

  1. Press Ctrl+Alt+S to open the IDE settings and then select Go | Imports.

  2. In the Exclude from auto-import and completion section, click the Add button Alt+Insert, and specify a class or a package that you want to exclude.

    You can also select whether you want to exclude items from the current project or from all projects (globally).

    Exclude from import and completion

Optimize imports

The Optimize Imports feature helps you remove unused imports and organize import statements in the current file or in all files in a directory at once according to the rules specified in Settings | Editor | Code Style | <language> | Imports.

Optimize all imports

  1. Select a file or a directory in the Project tool window (View | Tool Windows | Project).

  2. Do any of the following:

    • In the main menu, go to Code | Optimize Imports (or press Ctrl+Alt+O).

    • From the context menu, select Optimize Imports.

  3. (If you've selected a directory) Choose whether you want to optimize imports in all files in the directory, or only in locally modified files (if your project is under version control), and click Run.

Optimize imports in a single file

  1. Place the caret at the import statement and press Alt+Enter or use the the Intention action button icon.

  2. Select Optimize imports.

Remove use statement

Optimize imports when committing changes to Git

If your project is under version control, you can instruct GoLand to optimize imports in modified files before committing them to VCS.

  1. Press Ctrl+K or select Git | Commit from the main menu.

  2. Click Show Commit Options and in the commit message area, select the Optimize imports checkbox.

    Optimize imports before commit

Automatically optimize imports on save

You can configure the IDE to optimize imports in modified files automatically when your changes are saved.

  1. Press Ctrl+Alt+S to open the IDE settings and then select Tools | Actions on Save.

  2. Enable the Optimize imports option.

    Additionally, from the All file types list, select the types of files in which you want to optimize imports.

  3. Apply the changes and close the dialog.

Optimize imports when reformatting a file

You can tell GoLand to optimize imports in a file every time it is reformatted.

  • Open the file in the editor, press Ctrl+Alt+Shift+L, and make sure the Optimize imports checkbox is selected in the Reformat File dialog that opens.

    Reformat file dialog

After that every time you press Ctrl+Alt+L in this project, GoLand will optimize its imports automatically.

Use the goimports -local grouping

When you use this grouping, GoLand puts all imports that begin with the specified string after third-party packages.

  1. Open settings by pressing Ctrl+Alt+S and navigate to Editor | Code Style | Go. .

  2. Click the Imports tab.

  3. From the Sorting type list, select goimports.

  4. Select the Group checkbox.

  5. Click the Imports starting with radio button and type the grouping prefix in the text field.

    The goimports -local grouping
Last modified: 03 April 2024