Go: declared and not used: x / "fmt" imported and not used
Go refuses to compile code with unused local variables or unused imports.
Seen on:
Go
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
🧠 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.
Was this page helpful?
Report a correction or suggest an improvement
Last updated 7 Oct 2026