Go "declared and not used" / "imported and not used" - Fix in CI
Go makes unused local variables and unused imports a compile error, not a warning. Code that runs fine after a quick edit locally can fail CI the moment a variable or import is left dangling.
What this error means
The build stops with declared and not used: x or "fmt" imported and not used, naming the exact identifier or import. Nothing compiles until the unused declaration is removed or used.
./main.go:8:2: "errors" imported and not used
./main.go:14:2: declared and not used: resultCommon causes
An unused local variable
A variable is declared (often left over from a refactor or a commented-out line) but never read. Go rejects it outright.
An unused import
An import remains after the code that used it was removed, or was added speculatively. Go forbids unused imports.
How to fix it
Let goimports clean it up
goimports removes unused imports and adds missing ones automatically.
goimports -w .
go build ./...Remove or use the declaration
- Delete the unused variable, or use it where intended.
- If you need the side effect of an import only, use a blank import (
_ "package"). - For an intentionally-ignored value, assign to
_instead of a named variable.
How to prevent it
- Run
gofmt/goimportson save and in CI. - Use a pre-commit hook that runs
go build ./.... - Assign unused-but-required values to
_.