Skip to content
Latchkey

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

go vet warns when a struct literal from another package omits field names. Positional literals break silently when the struct gains or reorders fields, so vet flags them - and CI that gates on vet fails.

What this error means

A vet or test run fails with composite literal uses unkeyed fields. It commonly appears for literals of types from imported packages, especially after a dependency added a field.

go
./main.go:18:14: github.com/foo/bar.Config composite literal uses unkeyed fields

Diagnose it: toolchain, tags, or platform?

Go build failures that only appear in CI usually come from a different toolchain version, different build tags, or cross-compilation defaults that differ from your machine.

Terminal
go version
go env GOOS GOARCH CGO_ENABLED GOFLAGS GOTOOLCHAIN

# build exactly what CI builds, verbosely
go build -v ./... 2>&1 | tail -40

# CGO is the usual difference: on by default locally, often off in a slim CI image
CGO_ENABLED=0 go build ./...

Common causes

Positional literal of an imported struct

A struct from another package is initialized by position, so any field change silently shifts values.

vet gated in CI

go test runs vet by default, so the warning becomes a hard failure in the pipeline.

How to fix it

Use keyed fields

  1. Rewrite the literal to name each field so it is robust to struct changes.
Go
cfg := bar.Config{
	Host: "localhost",
	Port: 8080,
}

Run vet locally before pushing

  1. Run go vet to catch unkeyed literals before CI does.
Terminal
go vet ./...

How to prevent it

  • Always key struct literals of imported types.
  • Run go vet ./... before pushing.
  • Let your editor or gofmt enforce keyed fields.

Frequently asked questions

What causes Go vet "composite literal uses unkeyed fields"?
There are 2 common causes: positional literal of an imported struct and vet gated in ci. A struct from another package is initialized by position, so any field change silently shifts values.
How do I fix Go vet "composite literal uses unkeyed fields"?
There are 2 fixes depending on which cause you have: use keyed fields and run vet locally before pushing. Work through them in order, since the first is the most common.
What does Go vet "composite literal uses unkeyed fields" actually mean?
A vet or test run fails with composite literal uses unkeyed fields.
How do I stop Go vet "composite literal uses unkeyed fields" happening again?
Always key struct literals of imported types. The prevention section lists 3 changes that keep it from recurring.

Related guides

References

Not every red build is your code. Latchkey repairs the ones that are not, on the runner. Start free → 30-day trial · No credit card