Go Variable Naming Rules
You can give a variable a short name, such as x or y, or one that describes its purpose, such as age, price or carname.
Whichever you choose, Go has rules for names:
- The first character has to be a letter or an underscore (_)
- A digit can't be the first character
- Only letters, digits and underscores are allowed (
a-z, A-Z,0-9, and_) - Upper and lower case count as different, so age, Age and AGE are three separate variables
- Names can be as long as you like
- Spaces are not allowed
- Go keywords can't be used as names
Multi-Word Variable Names
A name made up of several words can be hard to read when they all run together.
These common styles make such names clearer:
Camel Case
The first word stays lowercase, and every word after it begins with a capital letter:
Camel Case syntax
main.go
myVariableName = "John"
Pascal Case
All the words begin with a capital letter, including the first:
Pascal Case syntax
main.go
MyVariableName = "John"
Snake Case
Underscores separate the words from one another:
Snake Case syntax
main.go
my_variable_name = "John"