imported and not used Go Origin: Application & API OSI layer 7 · Application Last updated 4 Nov 2026

Go: ./main.go:5:2: "os" imported and not used

The file imports a package that no code in that file uses; Go treats this as a compile error, not a warning.

Exact message you may see imported and not used"os" imported and not usedimported and not used: "fmt"./main.go: imported and not used
Seen on: Go

Meaning

Remove the import, or use it. If you import only for side effects (database drivers, image/png), use the blank identifier _. Tools like goimports or gopls add and remove imports on save.

Common causes

  • Import left over after refactoring
  • Debug code (fmt.Println) removed but import stays
  • Package used only in commented-out code
  • Driver needed for side effects only
  • Name collision with a local variable

⚡ Quick fix

  1. Delete the unused import line
  2. Use goimports -w . to fix automatically
  3. For side effects write import _ "github.com/lib/pq"

Detailed fix by platform

Go

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

How to diagnose

  1. Import — Which package named?
  2. Use — Referenced in this file?
  3. Side effect — Blank import intended?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

assignment mismatch: 1 variable but f returns 2 values — Go: assignment mismatch: 1 variable but os.Open returns 2 values (Go)assignment mismatch: …cannot assign to struct field m[k].f in map — Go: cannot assign to struct field users["bob"].Age in map (Go)cannot assign to stru…cannot use x as type in assignment/argument — Go: cannot use "5" (untyped string constant) as int value in argument to f (Go)cannot use x as type …declared and not used — Go: declared and not used: x / "fmt" imported and not used (Go)declared and not useddoes not implement interface (missing method) — Go: cannot use &Dog{} (value of type *Dog) as Animal value: *Dog does not implement Animal (missing method Speak) (Go)does not implement in…function main is undeclared in the main package — Go: runtime.main_main·f: function main is undeclared in the main package (Go)function main is unde…invalid operation: mismatched types — Go: invalid operation: a + b (mismatched types int and float64) (Go)invalid operation: mi…missing return — Go: ./main.go:18:1: missing return (Go)missing returnimported …

Explore the map of this error Full error map →

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