A Go internal package is a package whose import path contains a directory named internal. The Go toolchain only lets code rooted at the parent of that internal directory import it, so code outside that tree cannot depend on it. It is the standard way to keep helper code private to your module while still splitting it into packages.
The rule is enforced by the go command at build time, not by the language spec. If a forbidden import is attempted, the build fails with a clear error, and no flag turns the check off.
Take any import path that has internal as a path element, such as example.com/shop/internal/pricing. That package can be imported only by code whose import path starts with the parent of internal, which here is example.com/shop. Anything in example.com/shop/... may import it. Anything else may not.
The rule applies at any depth, not only at the module root. A directory example.com/shop/billing/internal/tax can be imported only from inside example.com/shop/billing/ and its subdirectories.
| Importer | Imports example.com/shop/internal/pricing |
|---|---|
example.com/shop/cmd/api | Allowed |
example.com/shop/billing | Allowed |
example.com/shop (root package) | Allowed |
example.com/other/app | Rejected |
github.com/someone/tool | Rejected |
Create a module with one internal package and one command that uses it.
Running go run ./cmd/api prints the result:
cmd/api lives under example.com/shop, which is the parent of internal, so the import is legal.
Now imagine a second module, example.com/other, that requires example.com/shop and tries the same import. The build stops before compiling:
The message names the package and says it is not allowed. You cannot work around it with a replace directive or a vendor copy of your module, because the check uses the import path, not where the files sit on disk. The only fix is for the owner to move the code out of internal, or for you to copy it into your own project, if the license allows.
The placement of the directory decides who can see the code. Two placements cover most projects.
example.com/shop/internal/... is visible to your whole module and invisible to every other module. This is the right default for application code and for libraries whose public API is only a few packages.
example.com/shop/billing/internal/tax is visible only inside billing. Use this when a subsystem has helpers that sibling subsystems such as shipping must not depend on. It gives you encapsulation inside a single module, which plain unexported identifiers cannot, because identifiers are private per package and a helper split across several packages needs to be exported.
The two mechanisms solve different problems.
| Mechanism | Scope of privacy | Granularity |
|---|---|---|
| Lowercase identifier | One package | A single function, type or field |
internal directory | A subtree of packages | A whole package |
Use lowercase names to hide details inside a package. Use internal when code has to be exported between your own packages but must not become part of a public API. Everything exported from a non-internal package of a published module becomes a compatibility promise that you keep until a new major version, so internal lets you refactor freely.
internal. Names like private or Internal have no effect.internal only restricts packages beneath it. A file such as internal.go in a normal package does nothing special.package pricing_test) in the same directory can too.internal later is a breaking change for anyone who imported it, so decide early for new code.Yes. The standard library uses it heavily, for example internal/abi and internal/poll under the Go source tree. Those packages cannot be imported by your programs, which is why they are free to change between releases.
Not directly. The rule is based on the import path, so a package in example.com/a/internal/x is not importable from example.com/b, even if you own both. Put the shared code in a normal exported package, or in a third module.
No. A workspace combines modules for building but does not change import path rules, so the check still applies between modules.
Most libraries benefit from it. Publish a small, deliberate public surface and keep implementation packages under internal, so you can change them without a major version bump.
The internal directory rule makes a package importable only from the tree rooted at its parent directory. The go command enforces it, so outside modules get a build error. Place internal at the module root to hide implementation packages from other modules, or nest it under a package to hide helpers from sibling packages. Use it together with lowercase identifiers, and keep your exported API small.