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 fieldsThe Fix
c := Config{
Host: "localhost",
Port: 8080,
}Step-by-Step Fix
- 1
Identify the unkeyed literal
Find struct literals that use positional values without field names.
- 2
Add field names
Prefix each value with the corresponding field name and a colon.
- 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