Skip to content
Latchkey

Go "declared and not used" build error - Fix in CI

Go treats an unused local variable as a compile error. Any local you declare must be read somewhere, or the build fails.

What this error means

A build fails with declared and not used: x. It commonly appears after refactors that remove the only use of a local, or after capturing a return value you no longer need.

go
./service.go:30:6: declared and not used: result

Common causes

Only use of a local removed

A refactor deleted the code that read the variable, leaving its declaration orphaned.

Captured a value you do not need

A multi-return call was assigned to a named variable that is never read.

How to fix it

Use, remove, or blank the variable

  1. Use the variable, delete its declaration, or assign the unwanted return to the blank identifier.
Go
_, err := doThing()  // discard the first return
if err != nil { return err }

Let vet catch it early

  1. Run go vet and go build locally so unused locals fail before CI.
Terminal
go build ./...

How to prevent it

  • Build locally before pushing so unused locals fail fast.
  • Use the blank identifier for return values you intentionally ignore.
  • Clean up locals as part of every refactor.

Frequently asked questions

What causes ""declared and not used""?
A refactor deleted the code that read the variable, leaving its declaration orphaned.
How do I fix "declared and not used"?
Use, remove, or blank the variable

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card