Restricting access to internal packages among siblings
0 reputation · 17 May 2023, 15:53 UTC
Internal package visibility constraints
In Go, the internal directory restricts package access to the parent directory and its subdirectories. While this effectively prevents external modules from Importing sensitive code, it does not provide a mechanism to restrict access between sibling packages located within the same internal tree.
When designing a modular architecture, it is common to expose utility logic to a subset of internal packages while keeping it hidden from other parts of the same module. The current directory-based enforcement is too coarse for this requirement, as any package within the path-level boundary can import any exported identifier from a sibling.
Is there an idiomatic way to limit exposure of an internal package to specific neighbors without restructuring the directory entirely? How can fine-grained visibility be enforced when the internal constraint is already in use?