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.
- Create a variable
productwith value"Coffee"and a variablepricewith value5 - At the second comment, raise
priceto6
The print lines are already written. The output should be exactly:
example.go
Coffee
5
6Stuck? Reveal a hint to help you.
Hints (0/4)
Key Takeaway
Next UpValues that never change.