Inspectopedia Help

Struct initialization without field names

Reports structures that are initialized without specifying their field names. By default, the inspection is available only when you use the type that is defined in a different package.

When initializing a structure, it is better to explicitly state field names in order to ensure that in case of changes in order of these fields or in names of the fields, they will correctly continue to be addressed.

Example:

_ = io.LimitedReader{nil, 10}

The LimitedReader initialization will be highlighted because explicit names for struct fields are missing. You can apply the Add keys and delete zero values quick-fix to the struct initialization. After the quick-fix is applied, the code looks as follows:

_ = io.LimitedReader{N: 10}

The inspection has the following options:

  • Report types defined in current package: reports situations when you declare and initialize struct in the same package. Consider the following example.

    d := struct { c string }{ "dsd "}
  • Report struct literals without types: reports fields without names when the type (struct or interface) is omitted. Consider the following example when the type is omitted.

    _ = []io.LimitedReader{ {nil, 10}, {nil, 20}, }

    In the following example, the type is present but is redundant.

    _ = []io.LimitedReader{ io.LimitedReader{nil, 10}, io.LimitedReader{nil, 20}, }

Locating this inspection

By ID

Can be used to locate inspection in e.g. Qodana configuration files, where you can quickly enable or disable it, or adjust its settings.

GoStructInitializationWithoutFieldNames
Via Settings dialog

Path to the inspection settings via IntelliJ Platform IDE Settings dialog, when you need to adjust inspection settings directly from your IDE.

Settings or Preferences | Editor | Inspections | Go | Code style issues

Inspection options

Here you can find the description of settings available for the Struct initialization without field names inspection, and the reference of their default values.

Report types defined in current package

Not selected

Report struct literals without types

Not selected

Availability

By default bundled with

GoLand 2024.1, Qodana for Go 2024.1,

Can be installed with plugin

Go, 241.SNAPSHOT

Last modified: 18 June 2024