A variable is a label stuck to a value
What a variable is, the difference between := and =, how x = x + 1 runs, and what a copy is: for people who have never written code.
🌐 Oʻzbekcha · Русский · English
Lesson GO-0.02 · Go: 0 → Guru · ▶️ Watch the video on YouTube · 💻 GitHub
This is the full written version of the video lesson: you can learn from it without watching (or hearing) the video. The video itself is in Uzbek.
What you will learn
In maths, x = x + 1 is wrong: no number can be one bigger than itself. In Go, the same line works fine. In this
lesson you will see why.
The one idea: a variable is a name label stuck to a value in memory. := creates a new label, and = replaces
the value under the label with a new one; the old value is gone.
By the end of the lesson you will be able to:
- read the lines
ochko := 3andochko = ochko + 1in plain words; - say what a program will print before you run it;
- read and fix the two most common compile errors (
undefinedandno new variables).
There are only three new words today:
| Sign or word | Plain meaning | Term |
|---|---|---|
| variable | a name label stuck to a value | variable |
:= (“colon equals”) | creates a new label | short variable declaration |
= (“let it be”) | puts a new value under a label | assignment |
Before you start
- GO-0.01 · What is a program?: what a program and a terminal are, what
go runandfmt.Printlndo, and how to read an error message. - You don’t need Go installed: copy any program from this page into go.dev/play and press Run.
- If you downloaded the course code, open a terminal in the lesson folder (
lessons/000-02-ozgaruvchi): the commands below run from there.
First, let’s recall the previous lesson. Our first program printed Salom, dunyo! (“Hello, world!” in Uzbek).
Which command did that?
Answer
fmt.Println. It prints whatever is inside the parentheses. Today we give it not a text but a number the program
has remembered.
A program needs to remember things
A program often has to remember a number, for example the score in a game. Picture a stadium scoreboard: it shows a team name, say PAXTAKOR, and next to it the number 3. When a goal is scored, the name stays where it is, the 3 is wiped and a 4 is written in its place.
PAXTAKOR 3 ⚽ goal! PAXTAKOR 4A program works the same way. While a program runs, its numbers sit in the computer’s memory, the place where it keeps what it needs to remember (like a phone’s memory). A number sitting in memory is called a value. We stick a label with a name on the value. A label stuck to a value is called a variable.
| Label (variable name) | Value (in memory) |
|---|---|
ochko | 3 |
The main rule: there is always exactly one value under one label. When a new value is put there, the old one is gone.
Why a “label” and not a “box”? Variables are often explained as “a box you put things in”. Felienne Hermans and colleagues compared the two pictures with 496 beginners: those taught with the “box” more often made the mistake of thinking a variable holds several values (as if the old value were still in the box). That is why we say “label”: like the name on the scoreboard, it is stuck to one place, and one number sits there.
:= creates a new label
Now let’s write this in Go (ochko is Uzbek for “score”):
ochko := 3The := sign (“colon equals”) creates a new label and sticks it to the value on the right. Read the line like
this: “a new label ochko, its value is 3”.
| After the line | Under ochko |
|---|---|
ochko := 3 | 3 |
On the next line we print the value:
fmt.Println(ochko)This time the word ochko has no quotes around it. So Println prints not the word “ochko” but the value
under that label, which is 3. With quotes (fmt.Println("ochko")), the word ochko itself would be printed.
We look at the difference between numbers and text in the next lesson.
One more small rule: := works only inside a function, that is, between the curly braces of func main() { … }.
All our code goes there.
= is assignment: ochko = ochko + 1
Now the puzzle of the lesson, with ochko in place of x:
ochko = ochko + 1The single = here is not the “equals” of maths. It puts a new value under a label that already exists. This is
called assignment. Read it as “let it be”: “let ochko now be ochko + 1”.
It is not an equation; it is a command. Go runs it in two steps (the official Go rules say so too: “The assignment proceeds in two phases”):
| Step | What happens | Under ochko |
|---|---|---|
| before | — | 3 |
| step 1 | the right side is computed first: Go looks at the label ochko, finds 3, adds 1: 3 + 1 = 4 | still 3 |
| step 2 | the result is put under the label on the left: the 3 is gone | 4 |
In short: a single = puts the result of the right side under the label on the left. To check whether two
things are equal, Go has a different sign, == (we meet it in GO-0.04).
The full program: create, print, change
Let’s put it all into one program. This is the file hisob/main.go from the course folder (hisob means
“count”):
package main
import "fmt"
func main() {
ochko := 3
fmt.Println(ochko)
ochko = ochko + 1
fmt.Println(ochko)
}What each line does:
| Line | Code | What it does |
|---|---|---|
| 1 | package main | the familiar template from the previous lesson: this is a program you can run |
| 3 | import "fmt" | brings in the package with the printing commands (fmt.Println) |
| 5 | func main() { | the program starts here |
| 6 | ochko := 3 | 1 · create: a new label ochko, value 3 |
| 7 | fmt.Println(ochko) | 2 · print: writes the value under the label to the screen |
| 8 | ochko = ochko + 1 | 3 · change: let ochko now be 3 + 1 |
| 9 | fmt.Println(ochko) | 4 · print again |
| 10 | } | main is over, the program is over |
Before running it, guess: what appears on the screen? A) 3 and 3 · B) 3 and 4 · C) 4 and 4 · D) an error
Answer
B: first 3, then 4. The first Println printed 3, because at that moment 3 was under the label. Then line 8
replaced the value with 4, and the second Println printed 4. If you chose “4 and 4”: the program runs the lines
from top to bottom, one at a time; when line 7 ran, line 8 had not run yet.
Let’s check in the terminal (go run translates the program and runs it straight away):
$ go run hisob/main.go
3
4What happened in memory, line by line:
| Line | Under ochko | On screen |
|---|---|---|
6 ochko := 3 | 3 | |
7 fmt.Println(ochko) | 3 | 3 |
8 ochko = ochko + 1 | 4 (the 3 is gone) | |
9 fmt.Println(ochko) | 4 | 4 |
One label, one value
Test yourself. This is the program savol/main.go (savol means “question”, yosh means “age”):
package main
import "fmt"
func main() {
yosh := 15
yosh = 16
fmt.Println(yosh)
}What does fmt.Println(yosh) print? A) 15 · B) 16 · C) 15 and 16
Answer
B: 16. Line 7 put 16 under the label yosh, and the 15 is gone. Someone who thinks of a variable as a box that
collects things might answer “both”, as if the old value were still in the box. But there is always one value under
a label.
$ go run savol/main.go
16A copy: rekord := ochko
Now we add a second label (rekord means “record”, the best score). This is nusxa/main.go (nusxa means
“copy”):
package main
import "fmt"
func main() {
ochko := 3
rekord := ochko
ochko = 0
fmt.Println(ochko, rekord)
}- Line 6: a new label
ochko, value 3. - Line 7:
rekord := ochko. Go makes a copy of the value ofochko, that is, a second number just like it, and the new labelrekordsticks to that copy. Now there are two separate 3s in memory. - Line 8:
ochko = 0, because a new game has started. - Line 9:
Printlntakes two values separated by a comma and prints both on one line, with a space between them.
Guess: A) 0 0 · B) 0 3 · C) 3 3
Answer
B: 0 3. ochko = 0 replaced only the value under the label ochko. rekord is stuck to its own copy, and
nobody touched it. If you chose “0 0”, you thought the two labels were stuck to the same value. With numbers, :=
always makes a separate copy.
$ go run nusxa/main.go
0 3The memory picture:
| After the line | ochko | rekord |
|---|---|---|
6 ochko := 3 | 3 | — (not yet) |
7 rekord := ochko | 3 | 3 (a copy) |
8 ochko = 0 | 0 | 3 (untouched) |
9 fmt.Println(ochko, rekord) | on screen: 0 3 |
ochko ──► [ 0 ] the 3 was wiped, 0 in its place
rekord ──► [ 3 ] its own copy: nobody touched itThis rule is for numbers. Later, with lists (slice) and dictionaries (map), we will see that a copy works differently (GO-1.08, GO-1.09).
Common mistakes
1. The label doesn’t exist yet, but = is used. The file xato/main.go is wrong on purpose (xato means
“error”):
package main
import "fmt"
func main() {
ochko = 0
fmt.Println(ochko)
}$ go run xato/main.go
# command-line-arguments
xato/main.go:6:2: undefined: ochko
xato/main.go:7:14: undefined: ochkoWe read the message as in the previous lesson: file xato/main.go, line 6, character 2, then
undefined: ochko, meaning “no name ochko was found”. Go doesn’t know this name because the label ochko has
not been created yet: = only puts a value under an existing label. The second line has the same cause (line 7
uses ochko again). Fix: the first time, write :=: ochko := 0.
2. The label exists, but := is used again. xato2/main.go:
package main
import "fmt"
func main() {
ochko := 0
ochko := 1
fmt.Println(ochko)
}$ go run xato2/main.go
# command-line-arguments
xato2/main.go:7:8: no new variables on left side of :=no new variables on left side of := means “there is not a single new variable on the left of :=”. ochko was
created on line 6, and it can’t be created a second time. Fix: on line 7, write a single =: ochko = 1.
Remember both mistakes with one rule:
| Situation | Which sign |
|---|---|
| the label doesn’t exist yet, we create it for the first time | := |
| the label exists, we change its value | = |
3. A variable that is created but never used. In Go this is an error too (you may meet it in the homework).
For example, if you write ochko := 3 and never use ochko anywhere:
func main() {
ochko := 3
fmt.Println("salom")
}$ go run unused/main.go
# command-line-arguments
unused/main.go:6:2: declared and not used: ochkoFix: use it (for example, fmt.Println(ochko)) or delete the line.
4. Mixing up quotes. fmt.Println("ochko") prints the word ochko itself, not the value, while
fmt.Println(ochko) prints the value:
$ go run quote/main.go
ochko
3(These two examples are not in the course folder: they were run separately just to show the mistake.)
Check yourself
Answer first, then open the answer.
1. What is the difference between := and =?
Answer
:= creates a new label and sticks it to a value. = puts a new value under an existing label (the old one is
gone).
2. How does ochko = ochko + 1 run?
Answer
In two steps: first the right side is computed (3 + 1 = 4), then the result is put under the label ochko on the
left, and the 3 is gone.
3. After rekord := ochko, if we change ochko, does rekord change too?
Answer
No. rekord is stuck to its own copy; ochko = 0 only replaces the value under ochko.
4. What does this code print?
a := 5
b := a
a = a + 2
fmt.Println(a, b)Answer
7 5. b got a copy of 5. Then a became 5 + 2, and nobody touched b.
5. From the previous lesson: what is the difference between fmt.Println("salom") and fmt.Println(salom)?
Answer
The first prints the word salom (“hello”) from inside the quotes. The second prints the value of a variable
named salom. If no such variable was created, the compiler says undefined: salom.
Homework
The starter files and the answers are in the lesson folder on GitHub: lessons/000-02-ozgaruvchi .
🟢 Easy (uyga/yashil/main.go): this file is the same as hisob/main.go. Rename ochko to yosh (“age”)
everywhere and write your own age instead of 3. The program should print your age now and next year. Extra:
change + 1 to + 5. What changes on the screen?
🟡 Harder (uyga/sariq/main.go): a := 3, b := 8. Replace the comment with lines that swap the values of a
and b, so that the program prints 8 3. Hint: first copy one of them into a third label.
Answers: the javob/
folder (javob means “answer”). Try it yourself first.
Answer
🟢 javob/yashil/main.go: with yosh := 15, the program prints 15 and 16 (your age instead of 15).
🟡 javob/sariq/main.go: vaqtincha := a (vaqtincha means “temporary”), then a = b, then b = vaqtincha →
8 3.
Why doesn’t a plain a = b followed by b = a work? After a = b, the 3 is gone and both labels have 8 under them:
$ go run swapbad/main.go
8 8Go also has a short way: a, b = b, a. It works correctly because Go first computes both values on the right and
only then puts them on the left (the same two-step rule).
Sources
- The official Go rules (the Go spec): Variables , Short variable declarations , Assignment statements
- The fmt.Println documentation · A Tour of Go: Short variable declarations
- Hermans, Swidan et al. (2018), Thinking out of the Box: Comparing Metaphors for Variables in Programming Education (summary )
- Ma, Ferguson, Roper, Wood (2007), Investigating the viability of mental models held by novice programmers, SIGCSE (paper )
- Harvard CS50x, lecture 1 notes (
counter = counter + 1) - All results were produced with
go1.26.8 linux/amd64on 26 November 2026.
Video and code
💻 Code and homework answers on GitHub
← Previous lesson: GO-0.01 · Next lesson: GO-0.03 → · All lessons
comments