Two questions about every name
When you name a variable, two different things matter. The first is whether Go accepts the name at all. That is a matter of rules, and a break in the rules stops the program from compiling. The second is whether people can understand the name. That is a matter of convention and judgement, and Go will run code with unclear names without complaint. This lesson covers both.
The rules Go enforces
- A name can contain letters, digits and underscores.
- It must not start with a digit.
- It must not contain spaces or symbols such as
-,$or.. - It can't be a keyword such as
for,func,if,varorreturn, because those words already have a meaning in the language.
Names that follow the rules: firstName, total2, _count, user_name. Names that break them: 1st (starts with a digit), first name (contains a space), for (a keyword). A name like 1st produces a syntax error:
syntax error: unexpected st at end of statement
Go compares identifiers literally, so letter case counts. name, Name and NAME are three different identifiers, and a program can declare all three at once. It usually shouldn't, because that is a recipe for confusion. If you misspell the case of a name, Go reports it as undefined.
The word "letters" is wider than English: Go also accepts letters from other alphabets, so café is a valid name. Stick to plain English letters anyway, since they are easy for everyone to type and read.
The convention: camelCase
Go programmers write multi-word names in camelCase: the first word is lowercase, and each following word starts with a capital letter, with no separators. That gives firstName, yearsOfExperience and isEmployed. Go does accept first_name, and the code runs the same way, but underscores in names are considered unidiomatic in Go, and the Go community writes names in camelCase.
One more convention is built into the language. At package level, a name that starts with a capital letter is exported, which means that other packages can use it. That is why you write fmt.Println with a capital P. For local variables inside a function, start with a lowercase letter.
Naming that reads well
- Say what the value is.
yearsOfExperienceexplains itself.yordata1do not. - Name booleans like questions. A prefix such as
isorhasshows the variable holdstrueorfalse:isEmployed,hasAccount. - Stay consistent. Mixing
firstNameandlast_namein one program works, but readers have to stop and wonder whether the difference means anything.
Short names are normal in Go when the scope is tiny. A loop counter called i is fine. The longer the variable lives, the more descriptive its name should be.
Unused names
A valid name is not enough on its own: Go also refuses to compile a function that declares a local variable it never uses, with the error declared and not used. Name only what you need.
Declaring with :=
The short declaration operator := creates a variable and infers its type from the value. firstName := "John" makes a string, isEmployed := true makes a bool and yearsOfExperience := 5 makes an int. It can only be used inside functions, and the name on its left must be new.
The exercise, step by step
- First name. Declare
firstNamewith:=and the value"John". Two words become one name, with a capital F in the second. - Employment flag. Declare
isEmployedwithtrue. Theisprefix tells readers this is a yes/no value. - Experience. Declare
yearsOfExperiencewith5. Three words, each after the first with a capital letter.
firstName := "John"
isEmployed := true
yearsOfExperience := 5
Key takeaways
- Names use letters, digits and underscores, and can't start with a digit.
- Keywords can't be used as names.
- Names are case-sensitive:
nameandNameare different. - Use camelCase for local variables, and name booleans like questions.
- Pick names that say what the value means.