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.
./service.go:30:6: declared and not used: resultCommon 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
- Use the variable, delete its declaration, or assign the unwanted return to the blank identifier.
_, err := doThing() // discard the first return
if err != nil { return err }Let vet catch it early
- Run go vet and go build locally so unused locals fail before CI.
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.