Start learning free

LESSON 11 OF 338 · VARIABLES

Variable Naming Rules in Go: Valid Names and camelCase

Lesson Read the full explanation

Variable names should be valid and easy to understand.

In Go, a variable name can contain letters, digits, and underscores, but it cannot start with a digit.

firstName := "John"
isEmployed := true
yearsOfExperience := 5

Go code commonly uses camelCase for local variable names, such as firstName and yearsOfExperience.

Variable names are case-sensitive: name, Name, and NAME are different identifiers.


Assignment
Follow the Coding Tutorial and create variables with clear names.


Hint
Use descriptive camelCase names.


STEP 1 OF 3
    Ctrl + Enter to check

    GO · VARIABLES

    Variable Naming Rules in Go, explained

    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, var or return, 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. yearsOfExperience explains itself. y or data1 do not.
    • Name booleans like questions. A prefix such as is or has shows the variable holds true or false: isEmployed, hasAccount.
    • Stay consistent. Mixing firstName and last_name in 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

    1. First name. Declare firstName with := and the value "John". Two words become one name, with a capital F in the second.
    2. Employment flag. Declare isEmployed with true. The is prefix tells readers this is a yes/no value.
    3. Experience. Declare yearsOfExperience with 5. 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: name and Name are different.
    • Use camelCase for local variables, and name booleans like questions.
    • Pick names that say what the value means.
    Keep going with GoAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free