Exported vs Unexported Names
Part of the Object Oriented Programming section of Coddy's GO journey — lesson 4 of 107.
In Go, the first letter of a name determines its visibility. Names starting with a capital letter are exported (accessible from other packages). Names starting with a lowercase letter are unexported (private to the package).
Exported names — visible outside the package
type User struct {
Name string // Exported field
Email string // Exported field
}
func CreateUser(name string) User { // Exported function
return User{Name: name}
}Unexported names — only visible inside the package
type secret struct { // Unexported struct
password string // Unexported field
}
func validate() bool { // Unexported function
return true
}Mixed — exported struct with some unexported fields
type Account struct {
Username string // Exported - accessible anywhere
Email string // Exported - accessible anywhere
password string // Unexported - only inside the package
}This naming convention replaces access modifiers like public and private found in other languages.
// Capital letter = Exported (public)
// Lowercase letter = Unexported (private)
func SaveData() {} // Exported
func loadData() {} // UnexportedFollowing this convention is essential in Go. When other packages import your code, they can only access names that start with a capital letter.
Challenge
MediumFix all the names in product.go to follow Go's exported naming convention. Capitalize the first letter of:
- The struct name:
product→Product - All field names:
name,price,stock - All function names:
totalValue,displayInfo - All field references inside the functions
Cheat sheet
In Go, the first letter of a name determines its visibility:
- Capital letter = Exported (accessible from other packages)
- Lowercase letter = Unexported (private to the package)
Exported names:
type User struct {
Name string // Exported field
Email string // Exported field
}
func CreateUser(name string) User { // Exported function
return User{Name: name}
}Unexported names:
type secret struct { // Unexported struct
password string // Unexported field
}
func validate() bool { // Unexported function
return true
}Mixed visibility:
type Account struct {
Username string // Exported - accessible anywhere
Email string // Exported - accessible anywhere
password string // Unexported - only inside the package
}This naming convention replaces access modifiers like public and private found in other languages.
Try it yourself
package main
import "fmt"
func main() {
var name string
var price float64
var stock int
fmt.Scan(&name, &price, &stock)
p := Product{
Name: name,
Price: price,
Stock: stock,
}
fmt.Println(DisplayInfo(p))
fmt.Printf("Total Value: %.2f\n", TotalValue(p))
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Object Oriented Programming
1Fundamentals of Go OOP
External FilesGo Workspace & ModulesPackages & ImportsExported vs Unexported NamesIntroduction to OOP in GoStructs as ClassesDefining Methods on StructsPointer vs Value ReceiversStruct InitializationConstructor FunctionsRecap - Simple Calculator4Interfaces
Introduction to InterfacesImplicit ImplementationInterface as ContractEmpty Interface (any)Type AssertionType SwitchInterface CompositionStringer & Error InterfacesRecap - Shape Calculator7Encapsulation
Exported vs Unexported FieldsPackage-Level EncapsulationGetter & Setter MethodsInformation Hiding in GoRecap - Student Records10Generics (Go 1.18+)
Introduction to GenericsType ParametersType ConstraintsGeneric StructsGeneric Methods WorkaroundRecap - Generic Collection2Types & Structs Deep Dive
Basic & Composite TypesCustom Type DefinitionsStruct TagsAnonymous StructsNested StructsZero Values & DefaultsRecap - Contact Book5Composition Over Inheritance
Why Go Has No InheritanceStruct Embedding BasicsMethod PromotionEmbedding Multiple StructsEmbedding vs AggregationShadowing Embedded MethodsRecap - Employee Hierarchy8Error Handling & OOP
The error InterfaceCustom Error TypesError Wrapping (fmt.Errorf)Sentinel Errorserrors.Is() and errors.As()Panic, Defer, and RecoverRecap - File Parser3Pointers & Memory
Pointer Basics in GoPointers to StructsPass by Value vs ReferenceThe new() FunctionGarbage Collection in GoRecap - Linked List Builder6Polymorphism in Go
Polymorphism via InterfacesDuck Typing in GoInterface Satisfaction RulesPolymorphic CollectionsDependency InjectionRecap - Payment Processor9Concurrency & OOP
Goroutines BasicsChannels & CommunicationBuffered vs Unbuffered ChanSelect Statementsync.Mutex & sync.RWMutexsync.WaitGroupThread-Safe Struct DesignRecap - Worker Pool