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}, }

Inspection options

Option

Type

Default

Report types defined in current package

Checkbox

false

Report struct literals without types

Checkbox

false

Inspection Details

Available in:

GoLand 2023.3

Plugin:

Go, 233.SNAPSHOT

Last modified: 13 July 2023