go compile_error ai_generated true

imported and not used

ID: go/imported-not-used

Also available as: JSON · Markdown
98%Fix Rate
95%Confidence
290Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1 active

Root Cause

Go requires all imports to be used. This is a compile error, not a warning.

generic

Workarounds

  1. 98% success Use goimports or gopls to auto-manage imports
    Use goimports or gopls to auto-manage imports

    Sources: https://pkg.go.dev/golang.org/x/tools/cmd/goimports

  2. 95% success Remove the unused import line
    // Simply delete the unused import line.
    // Or use goimports to auto-manage imports:
    // go install golang.org/x/tools/cmd/goimports@latest
    // goimports -w .
    
    // For side-effect imports, use blank identifier:
    import _ "image/png"

    Sources: https://go.dev/doc/effective_go#blank_import

Dead Ends

Common approaches that don't work:

  1. Comment out the import 60% fail

    Messy, easy to forget, and goimports will re-add it

  2. Use blank identifier _ for every unused import 55% fail

    Only correct for side-effect imports, not for temporarily unused ones

Error Chain

Leads to:
Preceded by:
Frequently confused with: