Unkeyed Fields in Struct Literal

composite literal uses unkeyed fields (govet)

Quick Answer

Use field names in your struct literal instead of positional values. This makes the code resilient to struct changes and more readable.

Why This Happens

While Go allows positional struct initialization, linters flag it because adding or reordering struct fields will silently break the initialization. Using named fields prevents this class of bugs and makes the code self-documenting.

The Problem

type Config struct {
    Host string
    Port int
}

c := Config{"localhost", 8080} // unkeyed fields

The Fix

c := Config{
    Host: "localhost",
    Port: 8080,
}

Step-by-Step Fix

  1. 1

    Identify the unkeyed literal

    Find struct literals that use positional values without field names.

  2. 2

    Add field names

    Prefix each value with the corresponding field name and a colon.

  3. 3

    Verify correctness

    Ensure each value is assigned to the correct field.

Bugsly catches this automatically

Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.

Try Bugsly free