What is the difference between exported and unexported identifiers in Go?
Understand Go visibility: uppercase names are exported (public), lowercase are unexported (package-private). Covers fields, methods, and JSON encoding.
Expected Interview Answer
In Go, an identifier is exported (publicly accessible from other packages) if its name begins with an uppercase letter, and unexported (private to its own package) if it begins with a lowercase letter.
This capitalization rule is Go's entire access-control mechanism, replacing the public/private/protected keywords found in other languages. It applies uniformly to variables, constants, functions, types, struct fields, and methods. Unexported identifiers can be used freely anywhere inside the same package but are invisible to importers, which lets a package expose a small deliberate API while hiding its internal implementation details.
- Simple, keyword-free access control
- Clear public API boundaries per package
- Encapsulation of internal implementation
- Consistent rule across all identifier kinds
- Encourages minimal, intentional exported surfaces
AI Mentor Explanation
Exported identifiers are like the players announced on the public team sheet that opponents and broadcasters can see and reference. Unexported ones are the private dressing-room tactics and net-practice notes: fully used by the team inside their own camp, but never visible to the opposing side, exactly as lowercase names stay hidden from other packages.
Step-by-Step Explanation
Step 1
Check the first letter
Uppercase first letter means exported; lowercase means unexported. That is the whole rule.
Step 2
Apply it to any identifier
The rule covers variables, constants, functions, types, struct fields, and methods identically.
Step 3
Use unexported internally
Lowercase identifiers are freely accessible anywhere within the same package.
Step 4
Expose a deliberate API
Capitalize only what other packages must use, keeping the public surface minimal.
Step 5
Watch struct fields in encoding
Only exported fields are marshaled by encoding/json, so unexported fields are skipped.
What Interviewer Expects
- Knowing capitalization controls visibility
- That the rule is package-scoped, not file-scoped
- It applies to fields and methods too
- Why unexported fields are ignored by json marshaling
- The encapsulation benefit of small public APIs
Common Mistakes
- Thinking visibility is file-based rather than package-based
- Believing Go has public/private keywords
- Forgetting struct fields must be exported for JSON encoding
- Assuming unexported means completely inaccessible even within the package
- Over-exporting identifiers that should stay internal
Best Answer (HR Friendly)
“In Go, whether something can be used by other parts of a program depends simply on how you name it: start it with a capital letter and it's public, start with a lowercase letter and it stays private to its own package. It's Go's simple way of deciding what to share versus keep internal.”
Code Example
package account
// Balance is exported and usable from other packages.
var Balance int
// secret is unexported and only visible inside this package.
var secret string
type User struct {
Name string // exported: marshaled to JSON
token string // unexported: skipped by encoding/json
}
// GetToken exposes the private field through a public method.
func (u User) GetToken() string {
return u.token
}Follow-up Questions
- Why are unexported struct fields ignored by encoding/json?
- Is Go's visibility scoped to the file or the package?
- How do you expose read-only access to an unexported field?
- Can a method be exported while its receiver type is unexported?
- How does the internal/ directory further restrict visibility?
MCQ Practice
1. Which identifier is exported in Go?
An identifier is exported only when its first letter is uppercase, so UserName is accessible from other packages.
2. What is the scope of an unexported identifier?
Unexported identifiers are visible throughout their entire package, not just the file they are declared in.
3. Why does encoding/json skip a struct field named `token`?
The json package uses reflection and can only access exported (capitalized) fields, so lowercase fields are ignored.
Flash Cards
How is an identifier exported in Go? — Its name begins with an uppercase letter, making it visible to other packages.
What does a lowercase first letter mean? — The identifier is unexported and only accessible within its own package.
What is Go's visibility scope? — Package level, not file level — unexported names are usable across all files of the package.
Why must JSON struct fields be capitalized? — encoding/json uses reflection and can only read exported fields.