IntelliJ IDEA 2024.2 Help

Code Style. Go

Use this page to configure formatting options for Go files. When you change these settings, the Preview pane shows how this will affect your code.

Tabs and Indents

Configure code style for indents, TAB and SPACE characters.

Item

Description

Use tab character

Use the Tab key for indentation. When the checkbox is cleared, IntelliJ IDEA uses spaces instead of tabs. By default, the checkbox is cleared.

Smart tabs

  • If this checkbox is selected, the indentation for nested code blocks will use tabs and spaces as needed, while alignment indentation will use only spaces.

  • If this checkbox is cleared, only tabs are used. This means that a group of spaces that fits the specified tab size is automatically replaced with a tab, which may result in breaking fine alignment.

The Smart tabs checkbox is available if the Use tab character option is enabled. By default, the checkbox is cleared.

Tab size

In this field, specify the number of spaces included in a tab. Default value: 4.

Indent

In this field, specify the number of spaces to be inserted for each indent level. Default value: 4.

Continuation indent

Specify the indentation for lines that continue from the previous line, making it clear that they are part of the same statement or block of code. Continuation indents are used when a single statement is too long to fit on one line.

Default value: 8.

Keep indents on empty lines

If this checkbox is selected, IntelliJ IDEA will keep indents on the empty lines as if they contained some code.

If this checkbox is cleared, IntelliJ IDEA will delete the tab characters and spaces.

By default, the checkbox is cleared.

Wrapping and Braces

Configure wrapping options and the usage of braces.

Item

Description

Hard wrap at

Use the Hard wrap at field to specify a margin space required on the right side of an element. If you select the Default option, then a value of the right margin from the global settings is used.

Wrap on typing

Use the Wrap on typing settings to specify how the edited text is fitted in the specified Hard wrap at:

  • Default: in this case IntelliJ IDEA uses the Wrap on typing option that is specified in the global settings.

  • Yes: in this case IntelliJ IDEA uses the value specified in the Right Margin field.

  • No: in this case this option is switched off and a line can exceed the value specified in the right margin.

Visual guides

Use the Visual guides field to specify multiple right margins. You can leave a default value or enter the number of spaces for your margin. If you want to specify several margins, enter numbers separated by comma.

Function call arguments

Select wrapping options for function call arguments. In Go, function call arguments are the values that are passed to a function when it is called.

You can select between the following options:

  • Do not wrap: when this option is selected, no special wrapping style is applied, the nested alignment and braces settings are ignored.

  • Wrap if long: select this option to have lines going beyond the right margin wrapped with proper indentation.

  • Wrap always: select this option to have all elements in lists wrapped so that there is one element per line with proper indentation.

  • Chop down if long: select this option to have elements in lists that go beyond the right margin wrapped so that there is one element per line with proper indentation.

You can apply the previous options to opening and closing parentheses:

  • New line after '(': add a new line after an opening parenthesis.

  • Place ')' on new line: add a new line before a closing parenthesis.

Composite literals

Select wrapping options for composite literals. In Go, a composite literal is a compact notation for creating a value of a composite type, which includes arrays, slices, maps, and structs.

You can select between the following options:

  • Do not wrap: when this option is selected, no special wrapping style is applied, the nested alignment and braces settings are ignored.

  • Wrap if long: select this option to have lines going beyond the right margin wrapped with proper indentation.

  • Wrap always: select this option to have all elements in lists wrapped so that there is one element per line with proper indentation.

  • Chop down if long: select this option to have elements in lists that go beyond the right margin wrapped so that there is one element per line with proper indentation.

You can apply the previous options to opening and closing parentheses:

  • New line after '(': add a new line after an opening parenthesis.

  • Place ')' on new line: add a new line before a closing parenthesis.

Function parameters

Select wrapping options for function parameters. In Go, function parameters are variables that are declared as part of a function signature and are used to pass data to the function when it is called. The values passed to the function as arguments are assigned to the function's parameters, allowing the function to operate on the data.

You can select between the following options:

  • Do not wrap: when this option is selected, no special wrapping style is applied, the nested alignment and braces settings are ignored.

  • Wrap if long: select this option to have lines going beyond the right margin wrapped with proper indentation.

  • Wrap always: select this option to have all elements in lists wrapped so that there is one element per line with proper indentation.

  • Chop down if long: select this option to have elements in lists that go beyond the right margin wrapped so that there is one element per line with proper indentation.

You can apply the previous options to opening and closing parentheses:

  • New line after '(': add a new line after an opening parenthesis.

  • Place ')' on new line: add a new line before a closing parenthesis.

Function result parameters

Select wrapping options for function parameters. In Go, a function can have one or more result parameters, which are declared in the function signature after the parameter list, and are used to return values from the function to the caller. The result parameters are specified using the return type(s) of the function.

You can select between the following options:

  • Do not wrap: when this option is selected, no special wrapping style is applied, the nested alignment and braces settings are ignored.

  • Wrap if long: select this option to have lines going beyond the right margin wrapped with proper indentation.

  • Wrap always: select this option to have all elements in lists wrapped so that there is one element per line with proper indentation.

  • Chop down if long: select this option to have elements in lists that go beyond the right margin wrapped so that there is one element per line with proper indentation.

You can apply the previous options to opening and closing parentheses:

  • New line after '(': add a new line after an opening parenthesis.

  • Place ')' on new line: add a new line before a closing parenthesis.

Imports

Configure code style rules for the import section.

Item

Description

Use backquotes for imports

Replace double quotes with backquotes.

Enabled

Disabled

go_use_backquotes_for_imports_cleared
Use backquotes for imports

Add parentheses for a single import

Add parentheses for the import section even when it has only one import statement.

Remove redundant import aliases

Removes the alias when the alias name matches dependency name. For example, the logrus alias will be removed from the following import entry:

import ( logrus "github.com/sirupsen/logrus" )

Sorting type

Select what style to use for sorting the import statements. You can select between the following options:

  • gofmt: sorts import statements in alphabetical order. Packages from standard libraries are mixed with third party packages.

    gofmt Sorting type
  • goimports: sorts import statements in a specific order, placing standard library packages first, then third-party packages, and then local packages.

    goimports Sorting type
  • none: does not sort import statements.

    none Sorting type

Move all imports to a single declaration

Groups all import statements into a single block at the top of the file.

Disabled

Enabled

Move all imports in a single declaration
Move all imports in a single declaration

Group packages from Go SDK

Group import statements that belong to Go SDK packages together. For example, import statements from packages like fmt, errors will be grouped together.

Disabled

Enabled

Group packages from Go SDK enabled
Group packages from Go SDK disabled

Note that this setting does not move import statements together if they are separated by statements from other packages. To group all import statements in a single section, select Move all packages to a single group.

Move all                         packages to a single group

Group

Group import statement according to the option that you chose. You can select the following options:

  • Current project packages: groups packages that belong to the current project. It means that this setting separates current project packages from any packages from external libraries (Go SDK, libraries from the go.mod file, files from the vendor directory, and so on).

  • Imports starting with: groups packages that start with a certain prefix. You can enumerate all the prefixes separating them with comma. All the corresponding packages will be moved to a single group.

    Imports starting with

Other

Item

Description

Add a leading space to comments

Add a space between double slashes and a comment text.

Disabled

Enabled

Add a leading space to comments
Add a leading space to comments

To exclude comments that start with a certain prefixes, click the Add icon (the Add icon), type the prefix, and click OK.

exclude comments that start with a certain prefixes

Column width for Fill paragraph

Specify the maximum number of characters allowed in a comment before the text is wrapped. The limit is applied to the comment text and redistributes lines, spaces, and line breaks in such a way so that the lines end up fitting within a certain maximum width. The default is 80 characters.

Before

After

Column width for Fill paragraph
Column width for Fill paragraph

On Reformat Code action

Automatically run gofmt when code is reformatted. By default, to reformat code, press Ctrl+Alt+L.

Last modified: 11 October 2024