An unexported identifier in Go is a name (such as a variable, constant, function, struct, interface, or method) that is inaccessible from outside its defining package. Go does not use access modifier keywords (likeDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
private or protected). Instead, an identifier is unexported if it meets either of two conditions: it is declared within a local block (such as a function body), or it is declared in the package block (or as a struct field/method) but its first character is a lowercase Unicode letter or the underscore character (_).
Syntax and Mechanics
To be exported, an identifier must be declared in the package block (or be a struct field or method) AND begin with an uppercase Unicode letter. All other identifiers are unexported. Note that the exact identifier_ is the blank identifier, while names like _internal are standard unexported identifiers that begin with the underscore character. Furthermore, identifiers declared inside a function are always unexported, even if they begin with an uppercase letter, because their lexical scope is restricted to the local block.
Visibility Rules
- Lexical Scope: Unexported package-level identifiers are accessible by any source file belonging to the same package. Go’s access control is package-level, not file-level. An unexported variable declared in
a.gocan be read and modified inb.goif both share the samepackagedeclaration. Conversely, local variables are restricted strictly to their local block (e.g., the function body) and are never accessible outside of it. - Struct Fields: Visibility applies independently to struct fields. An exported struct can contain unexported fields. External packages can instantiate the struct but cannot read, write, or initialize the unexported fields using struct literals.
- Methods and Interfaces: Unexported methods attached to an exported type cannot be invoked from outside the package. If an exported interface contains an unexported method, a type defined in an external package cannot implement that interface. This is because the external type cannot define the unexported method belonging to the interface’s original package.
- Type Aliases: If an exported type alias points to an unexported type, the type itself remains unexported, but the alias allows external packages to interact with it under the exported name’s constraints.
Cross-Package Behavior
When an external package imports a package containing unexported identifiers, the compiler prevents any direct reference to those identifiers.Reflection and Unexported Fields
Thereflect package can inspect unexported fields of a struct, but it cannot mutate them. Attempting to set a value on an unexported field via reflection will result in a runtime panic.
Master Go with Deep Grasping Methodology!Learn More





