Go Variable Naming Rules
Go (Golang) – Variable Naming Rules
Go follows strict and simple naming conventions to keep code clean, readable, and consistent. Understanding these rules is very important.
1️⃣ Basic Naming Rules
✔ Must start with a letter (a–z, A–Z) or underscore (_)
✔ Can contain letters, numbers, and underscores
❌ Cannot start with a number
❌ Cannot use special characters (@, #, $, etc.)
✅ Valid Names
❌ Invalid Names
2️⃣ Case Sensitivity
Go is case-sensitive.
👉 These are treated as two different variables.
3️⃣ Exported vs Unexported Variables (Very Important)
🔹 Exported (Public)
-
Starts with a capital letter
-
Accessible outside the package
🔹 Unexported (Private)
-
Starts with a small letter
-
Accessible only within the package
4️⃣ Naming Style (Best Practices)
✔ Use camelCase for local variables
✔ Use PascalCase for exported variables
❌ Avoid snake_case (not idiomatic Go)
5️⃣ Short & Meaningful Names
✔ Use short names for small scopes
✔ Use meaningful names for clarity
❌ Avoid unclear names
6️⃣ No Reserved Keywords Allowed
Go keywords cannot be used as variable names.
❌ Invalid
Some Go keywords:
7️⃣ Use _ (Blank Identifier) Properly
The underscore _ is used to ignore values, not as a normal variable name.
8️⃣ Examples (Good vs Bad)
❌ Bad
✅ Good
Summary
✔ Start with letter or _
✔ Case-sensitive
✔ Capital letter = public
✔ Small letter = private
✔ Avoid special characters & keywords
✔ Use camelCase / PascalCase
