Variables
A variable is a labeled box.
You put a value inside the box. Later you can look at it or change it.
The label is the name. The value is what sits inside.
In Go you make a box with var. Inside a function you can also use := .
Declaring with var
You can write the type yourself.
Or you can let Go guess the type from the value.
If you make a variable and never use it, Go stops with an error. It wants tidy programs.
Short declaration :=
Inside functions, := is the common way.
It makes a new labeled box and fills it in one step.
Go picks the type from the right side.
Changing what is in the box
Use = to put a new value in a box that already exists.
:= is for making a new box. = is for updating an old one.
Zero values
If you make a box but leave it empty, Go still puts a starter value inside.
Numbers get 0. Bool gets false. String gets an empty string.
Think of it as a starter sticker so the box is never scary and blank.
Many boxes at once
You can declare several variables together.
Keep names clear so you remember what each box holds.
Use := for new local boxes. Use = only to update a box that already exists.
Test yourself
Three quick questions made just for this lesson. Earn 10 XP per correct answer.