go naming ai_generated true

could not import — Go package in directory with hyphen causes import path mismatch

ID: go/hyphen-in-package-directory-name

Also available as: JSON · Markdown
85%Fix Rate
88%Confidence
4Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1 active

Root Cause

Go package directory named with hyphens (my-package) creates a mismatch because Go package names cannot contain hyphens.

generic

Workarounds

  1. 95% success Rename directory to use underscores or no separator, match package declaration name
    mv my-package mypackage  # directory name should match or be importable as package name

    Sources: https://go.dev/doc/effective_go#package-names

  2. 82% success If external module, import by module path and alias the package name
    import mypackage "github.com/org/my-package/subpkg"  // alias resolves the hyphen

    Sources: https://go.dev/ref/spec#Import_declarations

Dead Ends

Common approaches that don't work:

  1. Use the directory name as-is in package declaration 90% fail

    Go compiler rejects hyphens in package names; package mypackage in dir my-package causes import confusion

  2. Use replace directive to map hyphenated path 80% fail

    Replace directive remaps modules, not individual packages within a module; does not fix the naming issue