Skip to content
Latchkey

Go vet "composite literal uses unkeyed fields" - Fix in CI

The composites vet check flags struct literals from other packages that set fields positionally instead of by name. Unkeyed literals silently break when the upstream struct adds or reorders a field, so vet (and golangci-lint’s govet) fail the build on them.

What this error means

A go vet ./... step fails with <Type> composite literal uses unkeyed fields, naming a struct from an imported package. The code compiles; vet is warning that the positional literal is fragile.

go vet output
./main.go:14:10: github.com/org/lib.Options composite literal
	uses unkeyed fields

Common causes

A positional struct literal from another package

Writing lib.Options{"a", 5, true} instead of lib.Options{Name: "a", Size: 5, On: true} depends on the field order, which the upstream package can change.

A generated or copied literal that drifted

A literal copied from an older struct definition, or generated against a previous version, no longer matches and vet catches the fragility.

How to fix it

Key the fields by name

Name each field so the literal is robust to reordering and additions.

Go
opts := lib.Options{
	Name: "a",
	Size: 5,
	On:   true,
}

Run vet/golangci-lint to find every occurrence

Terminal
go vet ./...
# or via golangci-lint's govet
golangci-lint run --enable govet ./...

How to prevent it

  • Always use keyed fields for struct literals from other packages.
  • Keep go vet/govet in CI so unkeyed literals fail PRs.
  • Regenerate code against the current struct definitions.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →