declared and not used 🐹 Go

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

  1. Remove the variable/import
  2. Assign to the blank identifier _ while debugging
  3. Run goimports / gopls on save

Detailed fix by platform

Shell

  1. go
    go install golang.org/x/tools/cmd/goimports@latest
    goimports -w .

How to diagnose

  1. Line — Variable/import named
  2. 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:

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