# Go "declared and not used" / "imported and not used" - Fix in CI

> Fix Go "declared and not used" and "imported and not used" compile errors in CI - Go treats unused variables and imports as hard errors, not warnings.

Source: https://latchkey.dev/learn/go/go-declared-and-not-used  
Updated: 2026-06-25

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.

## 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 ./...
```

> A package that imports C fails only when `CGO_ENABLED=1`, and the default differs between environments and base images. Set it explicitly in CI rather than inheriting it.

## FAQ

### What causes Go "declared and not used" / "imported and not used"?

There are 2 common causes: an unused local variable and an unused import. A variable is declared (often left over from a refactor or a commented-out line) but never read.

### How do I fix Go "declared and not used" / "imported and not used"?

There are 2 fixes depending on which cause you have: let goimports clean it up and remove or use the declaration. Work through them in order, since the first is the most common.

### What does Go "declared and not used" / "imported and not used" actually mean?

The build stops with declared and not used: x or "fmt" imported and not used, naming the exact identifier or import.

### How do I stop Go "declared and not used" / "imported and not used" happening again?

Run gofmt/goimports on save and in CI. The prevention section lists 3 changes that keep it from recurring.

---

Latchkey runs CI/CD that repairs its own failures. Agent entry points: https://latchkey.dev/agent.txt, https://latchkey.dev/openapi.json, https://latchkey.dev/llms.txt
