Uppercase vs Lowercase Naming in Go: Key Differences and Usage
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.
| Factor | Uppercase Naming | Lowercase Naming |
|---|---|---|
| Visibility | Exported (public) outside the package | Unexported (private) inside the package |
| Access | Accessible from other packages | Accessible only within the same package |
| Use Case | Functions, types, variables meant for external use | Internal helper functions, variables, types |
| Convention | First letter is uppercase | First letter is lowercase |
| Example | MyFunc, MyType | myFunc, 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.
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) }
Lowercase Equivalent
Example showing unexported function and type using lowercase naming, accessible only within the package.
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) }
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.