Go identifiers and reserved keywords
Names and tokens
A token is a meaningful unit of source code. An identifier names something, such as a variable. Go identifiers start with a Unicode letter or underscore and may continue with letters, underscores and Unicode decimal digits. A digit cannot be first. Keywords such as var, func and package are reserved. In this example score is a name, var a keyword, = punctuation and 7 an integer literal. The var declaration creates a variable; we will study its forms in lesson 3.
package main
import "fmt"
func main() { var score = 7 fmt.Println(score)}The program prints 7, the value associated with score. Identifiers are case-sensitive. score and Score are different names. Uppercase initial letters also matter for visibility across packages, a topic covered later.
Choose meaningful names
package main
import "fmt"
func main() { var item2 = 4 var Item2 = 8 fmt.Println(item2, Item2)}The output is 4 8. Names such as item2 and totalPrice are valid; 2item, total-price and var are not variable names. Predeclared names such as int and true are not keywords, but avoid reusing them because doing so hides their usual meaning. The single underscore _ has special behavior, covered in lesson 7.
Try it locally
Declare a variable named orderCount with value 12 and print it. Save as main.go and run go run main.go. Expected: 12. Hint: start the name with a letter; use var before the name.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Tokens and Declarations 01: Go identifiers and reserved keywords
Check the lesson objective before moving on.
Which is a valid Go variable name?
Which word is a reserved keyword?