Skip to content
Latchkey

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.

go build output
./main.go:8:2: "errors" imported and not used
./main.go:14:2: declared and not used: result

Common 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.

Terminal
goimports -w .
go build ./...

Remove or use the declaration

  1. Delete the unused variable, or use it where intended.
  2. If you need the side effect of an import only, use a blank import (_ "package").
  3. For an intentionally-ignored value, assign to _ instead of a named variable.

How to prevent it

  • Run gofmt/goimports on save and in CI.
  • Use a pre-commit hook that runs go build ./....
  • Assign unused-but-required values to _.

Related guides

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