Go: declared and not used: x / "fmt" imported and not used
Go refuses to compile code with unused local variables or unused imports.
declared and not usedimported and not useddeclared but not used Meaning
This keeps code clean, but is annoying while debugging. Use the variable, remove it, or assign to _; for imports, remove them or let goimports manage them automatically.
Common causes
- Variable assigned but never read
- Leftover import after editing code
- Shadowed variable with := inside a block
⚡ Quick fix
- Remove the variable/import
- Assign to the blank identifier _ while debugging
- Run goimports / gopls on save
Detailed fix by platform
Shell
- go
go install golang.org/x/tools/cmd/goimports@latest goimports -w .
How to diagnose
- Line — Variable/import named
- Shadowing — := inside if/for blocks?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
Most viewed in Go
Other ways to find it
Where this error sits
Origin: Application & API. Errors that are often confused with or caused by this one (click to open):
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
💬 What worked for others
No tips yet — did something else fix this for you?
Share what worked for you
Tips are checked by an editor before they appear. Links are removed.
Report a correction or suggest an improvement
Last updated 15 Oct 2026