Remembering Things

Remembering Things

Remembering Things

Variables let you name values and reuse them later.

example.go
name := "Gopher"
age := 10
happy := true

:= creates a new variable. It's called a short variable declaration. No type keywords needed. Go looks at the value and picks the type for you. "Gopher" makes name a string. 10 makes age an int. true makes happy a bool.

Changing a Variable

= changes a variable that already exists:

example.go
status := "pending"
status = "shipped"
fmt.Println(status) // shipped

Using = on a variable that doesn't exist yet fails with undefined: status. Creating a variable and never reading it fails with declared and not used: status.

>_Exercise

Exercise: Order Receipt

The price of a coffee goes up while the receipt is printing.

  1. Create a variable product with value "Coffee" and a variable price with value 5
  2. At the second comment, raise price to 6

The print lines are already written. The output should be exactly:

example.go
Coffee
5
6
Stuck? Reveal a hint to help you.
Hints (0/4)
Key Takeaway
Next UpValues that never change.