Naming Rules

Naming Rules #

Choosing good variable names is essential for writing clean and maintainable code.
Go provides flexible rules for naming variables, but following best practices improves code readability.


✅ Basic Naming Rules #

  • A variable name must start with a letter (a-z, A-Z) or an underscore (_).
  • A variable name cannot start with a digit.
  • A variable name can only contain letters, digits, and underscores (a-z, A-Z, 0-9, _).
  • Variable names are case-sensitive.
    Example:
    var age int    // different from Age or AGE
  • There is no limit on the length of the variable name.
  • A variable name cannot contain spaces.
  • A variable name cannot be any of Go’s reserved keywords Examples of keywords: var, func, if, else, return, etc.

✅ Example of Valid and Invalid Names #

Variable NameValid?Explanation
ageValid simple name
_counterUnderscore is allowed at the start
1numberCannot start with a digit
my-variableHyphen is not allowed
totalPriceValid camelCase style
returnCannot be a keyword

✅ Multi-Word Variable Naming Conventions #

When naming variables with multiple words, readability matters a lot. Here are the common conventions:

📚 Camel Case (Most Common in Go) #

  • The first word is lowercase, and subsequent words start with an uppercase letter.
var myVariableName = "John"

📚 Pascal Case #

  • Every word starts with a capital letter.
var MyVariableName = "John"

Typically used for exported variables or types.

📚 Snake Case #

  • Words are separated by underscores.
var my_variable_name = "John"

Less common in Go but still acceptable for readability in some cases.


✅ Best Practices #

  • Use meaningful names that reflect the purpose of the variable. ✅ Example:

    var userAge int
    var productPrice float64
  • Avoid overly short names unless in small scopes, e.g., loops: ✅ Example:

    for i := 0; i < 10; i++ { ... }
  • Consistently use one naming style throughout your project for clarity.


🚀 Follow these naming rules and conventions to make your Go code clean, readable, and maintainable.