0
0
GoComparisonBeginner · 3 min read

Uppercase vs Lowercase Naming in Go: Key Differences and Usage

In Go, uppercase names make identifiers exported and accessible outside their package, while lowercase names keep them unexported and private to the package. This naming convention controls visibility and encapsulation in Go programs.
⚖️

Quick Comparison

Here is a quick comparison of uppercase and lowercase naming in Go based on key factors.

FactorUppercase NamingLowercase Naming
VisibilityExported (public) outside the packageUnexported (private) inside the package
AccessAccessible from other packagesAccessible only within the same package
Use CaseFunctions, types, variables meant for external useInternal helper functions, variables, types
ConventionFirst letter is uppercaseFirst letter is lowercase
ExampleMyFunc, MyTypemyFunc, myType
⚖️

Key Differences

In Go, the first letter of an identifier determines its visibility. If it starts with an uppercase letter, it is exported and can be accessed from other packages. This is how Go controls what parts of your code are public API versus internal implementation.

Lowercase names are unexported, meaning they are private to the package they are declared in. This helps keep internal details hidden and prevents accidental use from outside code.

This naming rule applies to all identifiers including functions, variables, constants, types, and struct fields. It is a simple but powerful way to enforce encapsulation without extra keywords.

⚖️

Code Comparison

Example showing an exported function and type using uppercase naming.

go
package main

import "fmt"

// Exported function
func Greet() {
    fmt.Println("Hello from exported function!")
}

// Exported type
type Person struct {
    Name string
}

func main() {
    Greet()
    p := Person{Name: "Alice"}
    fmt.Println(p.Name)
}
Output
Hello from exported function! Alice
↔️

Lowercase Equivalent

Example showing unexported function and type using lowercase naming, accessible only within the package.

go
package main

import "fmt"

// Unexported function
func greet() {
    fmt.Println("Hello from unexported function!")
}

// Unexported type
type person struct {
    name string
}

func main() {
    greet()
    p := person{name: "Bob"}
    fmt.Println(p.name)
}
Output
Hello from unexported function! Bob
🎯

When to Use Which

Choose uppercase names when you want your functions, types, or variables to be accessible from other packages, such as public APIs or libraries. Use lowercase names to keep implementation details private within a package, helping maintain clean and safe code boundaries.

Always start with lowercase for internal helpers and only export what is necessary to avoid exposing unnecessary details.

Key Takeaways

Uppercase names in Go are exported and accessible outside their package.
Lowercase names are unexported and private to their package.
This naming rule applies to all identifiers: functions, types, variables, and fields.
Use uppercase for public APIs and lowercase for internal implementation.
This simple convention enforces encapsulation without extra syntax.