Numbers and text: why 2 + 2 and "2" + "2" are different
What a value's type is, the difference between int, float64 and string, what + does with each, and how to read a mismatched types error.
🌐 Oʻzbekcha · Русский · English
Lesson GO-0.03 · Go: 0 → Guru · ▶️ video coming soon · 💻 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
On a calculator, 2 + 2 is always 4. In a Go program, something that looks just like 2 + 2 can print 22. The
difference hides in one small character. In this lesson you will find that character.
The one idea: every value has a type. A value written in quotes is text, and a whole number without quotes is a number. Plus works only with two values of the same type: it adds numbers and joins texts. If you mix them, Go does not translate the program.
By the end of the lesson you will be able to:
- say which type
15and"15"have; - say in advance what
+does with two numbers and with two texts; - read the
mismatched typeserror and fix it withstrconv.Itoa; - explain why
7 / 2gives3in Go and how to get3.5.
Today’s new words:
| Word | Plain meaning | Term |
|---|---|---|
| type | the kind of a value: do we calculate with it, or do we only write it down | type |
int | a whole number: 15, 7, 2 | int (integer) |
string | text, written in quotes in code: "15", "Salom" | string |
There are two helper words too: float64 (a number with a point, a fraction) and strconv.Itoa (makes text from a
whole number). Each is explained in its own section.
Before you start
- GO-0.01 · What is a program?: what a program is,
go run, thepackage main/import/func maintemplate,fmt.Println, and how to read an error message (file:line:character). - GO-0.02 · A variable is a label stuck to a value: a variable (a label), the difference between
:=and=. Today we look not at the labels but at the values themselves. - 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-03-son-va-matn): the commands below run from there.
First, let’s recall the previous lesson. What do these lines print?
ochko := 3
ochko = ochko + 1
fmt.Println(ochko)A) 3 · B) 4 · C) an error
Answer
B: 4. In the first line the label ochko (“score”) was stuck to the value 3. We read the second line as “let
ochko now be ochko + 1”: the value under the label changed to 4, and 3 was gone. The third line prints 4.
| After the line | Under ochko | On screen |
|---|---|---|
ochko := 3 | 3 | |
ochko = ochko + 1 | 4 | |
fmt.Println(ochko) | 4 | 4 |
Every value has a type
Picture a form with two fields: your age and your phone number. Both are made of digits.
Age: 15 → +1 → 16 (your age next year)
Phone: 90 123 45 67 → +1 → ? (you don't calculate with a phone number)Add one to your age and you get your age next year. You don’t calculate with a phone number: you only write it down. So even when two values are made of the same digits, they are of two different kinds.
The computer also has to know the kind of every value: does it add it, or does it only write it down? In programming, the kind of a value is called its type. In Go, every value has a type.
Today we meet two main types:
- In Go, the main type for whole numbers is called
int. The15inyosh := 15(“age”) has no quotes, so it is anint. - Text is called
string. Like"Salom, dunyo!"(“Hello, world!”) in the first lesson, we write text in quotes in code. The"15"inyozuv := "15"is in quotes, so it is astring, just like the phone number.
yosh := 15
yozuv := "15"(yozuv means “text, writing”.) The memory picture:
| Label | Value (in memory) | Type | Why |
|---|---|---|---|
yosh | 15 | int | a whole number, no quotes |
yozuv | "15" | string | it has quotes: these are two characters, 1 and 5 |
Both contain 15, but they are two different values. Go knows the type from how the value is written: with quotes it
is a string, a whole number without quotes is an int.
Plus looks at the type first
Now let’s try plus with both types. This is the file qoshish/main.go (“addition”) from the course folder:
package main
import "fmt"
func main() {
fmt.Println(2 + 2)
fmt.Println("2" + "2")
}| Line | Code | What it does |
|---|---|---|
| 1 | package main | the template from the first lesson: this is a program that can run |
| 3 | import "fmt" | connects the fmt box (package) with the tools for printing |
| 5 | func main() { | the program starts here |
| 6 | fmt.Println(2 + 2) | two plus two without quotes: two int values |
| 7 | fmt.Println("2" + "2") | two in quotes plus two in quotes: two string values |
| 8 | } | main is over, the program is over |
fmt.Println prints whatever is inside the parentheses. This program prints two lines. Guess before you run it:
A) 4 and 4 · B) 4 and 22 · C) an error
Answer
B: 4 and 22. The small character from the start was the quotes. In line 6 both values are int, so they were
added. In line 7 both values are string, so they were joined side by side. If you chose “4 and 4”, you did not
notice the quotes. If you chose “an error”, you thought texts can’t be combined with plus: they can, but the result is
joined text, not a sum.
Let’s check in the terminal. The go run command translates the file main.go in the qoshish folder and runs it
right away:
$ go run qoshish/main.go
4
22Why? A value written in quotes is text. And plus first looks at the type on each side:
| Code | Left side | Right side | What + does | Result |
|---|---|---|---|---|
2 + 2 | int | int | adds | 4 (a number) |
"2" + "2" | string | string | joins side by side | "22" (text) |
Println prints text without the quotes, so the 22 on the screen looks like a number. In fact it is not the number
twenty-two, but two 2 characters side by side:
"2" + "2" → "22"
[2] [2] [2][2] two characters, not a numberIn short, plus works only with two values of the same type. If both are int, it adds them. If both are string, it
joins them. Joining texts like this is called string concatenation.
A small test: hour + minute
Now let’s check that the rule works with variables too. sinov/main.go (“test”):
package main
import "fmt"
func main() {
soat := "10"
daqiqa := "5"
fmt.Println(soat + daqiqa)
}- Line 6: a new label
soat(“hour”), its value is"10"in quotes. - Line 7: a new label
daqiqa(“minute”), its value is"5"in quotes. - Line 8: combines the values under the two labels with plus and prints the result.
There are no quotes on this line. But plus looks at the type of the values, not at how the line is written. Guess: A) 15 · B) 105 · C) an error
Answer
B: 105. Under soat and daqiqa there are values in quotes, that is, string values. Plus joined them: "10"
and "5" → "105". This is not the number one hundred and five, but three characters side by side: 1, 0, 5. If
you chose “15”, you looked at the missing quotes in line 8. But the type travels with the value: under the label there
is "10".
$ go run sinov/main.go
105| After the line | soat | daqiqa | On screen |
|---|---|---|---|
6 soat := "10" | "10" (string) | — | |
7 daqiqa := "5" | "10" | "5" (string) | |
8 fmt.Println(soat + daqiqa) | "10" | "5" | 105 |
Mixing types: an error
Now let’s mix the two types. aralash/main.go (“mixed”):
package main
import "fmt"
func main() {
gap := "Yosh: "
yosh := 15
fmt.Println(gap + yosh)
}- Line 6: under
gap(“sentence”) there is, in quotes, the wordYosh(“Age”), a colon and one space at the end. This is astring. - Line 7: under
yoshthere is15without quotes, that is, anint. - Line 8: we want to combine them with plus.
Will this program run? Remember: plus works only with values of the same type, and here we have a string and an
int. Guess: A) Yosh: 15 · B) 15 · C) an error
Answer
C: an error. The types on the two sides of plus are different: string on the left, int on the right. Go does
not know which job to do: add or join. If you chose “Yosh: 15”, you thought Go turns the number into text by itself.
Some languages do that, but Go does not (we will see why below).
$ go run aralash/main.go
# command-line-arguments
aralash/main.go:8:14: invalid operation: gap + yosh (mismatched types string and int)The compiler (our translator) did not translate the program, so the program did not run. There is no output from the program on the screen, only the error message.
Reading the error message piece by piece
| Piece | Meaning |
|---|---|
# command-line-arguments | the name Go gives the program when go run gets a file name; you can skip this line |
aralash/main.go | the error is in this file |
8 | the line number: line 8 |
14 | the character position in the line: the word gap starts exactly here |
invalid operation | ”wrong operation” |
gap + yosh | which operation is wrong |
(mismatched types string and int) | the reason in parentheses: “the types don’t match: one is string, one is int” |
How 14 is counted: the one tab at the start of the line is one character too. Then fmt.Println( is 12 characters
(2–13). So gap starts at character 14.
Why doesn’t Go allow this?
Some other languages silently turn the number into text. For example, in one of them "2" + 2 is not an error, it
gives "22". Then in a shop program "2000" + 2000 could become the text "20002000" instead of four thousand. Such
a mistake can go unnoticed.
Go checks the types while it translates. It shows the error before the program starts. Go’s official rules (the Go spec) say: “the operand types must be identical”, that is, the types on the two sides of an operation must be the same. (This rule has a small exception; we look at it in the “Going deeper” section.)
The fix: strconv.Itoa turns a number into text
To fix it, we turn the number into text. Go has another ready-made toolbox (package) for this, called
strconv. We import it just like fmt. aylantir/main.go (“convert”):
package main
import "fmt"
import "strconv"
func main() {
gap := "Yosh: "
yosh := 15
fmt.Println(gap + strconv.Itoa(yosh))
}Only two lines differ from aralash/main.go:
| Line | Code | What it does |
|---|---|---|
| 4 | import "strconv" | new: connects the strconv box |
| 7–8 | gap := "Yosh: ", yosh := 15 | as before: a string and an int |
| 9 | fmt.Println(gap + strconv.Itoa(yosh)) | changed: strconv.Itoa(yosh) creates a new text from the whole number in the parentheses |
Read strconv.Itoa from the inside out. First, the 15 under yosh is taken. strconv.Itoa makes a new text
"15" from it. yosh stays an int: nobody touched it. Then plus joins the two texts.
| Code | What happens | Type |
|---|---|---|
gap | "Yosh: " | string |
yosh | 15 (does not change) | int |
strconv.Itoa(yosh) | "15" (a new text) | string |
gap + strconv.Itoa(yosh) | "Yosh: " + "15" → "Yosh: 15" | string |
What will it print? Guess: A) Yosh: 15 · B) Yosh:15 · C) an error
Answer
A: Yosh: 15. Now there is a string on both sides of plus, so Go translates the program. The space between the
colon and 15 was at the end of the gap text. If you chose “Yosh:15”, you missed that space.
$ go run aylantir/main.go
Yosh: 15In short: if the types don’t match, Go gives an error. And strconv.Itoa turns a whole number into text.
Division: why 7 / 2 gives three
Now let’s divide. We share seven apples equally between two children, but we don’t cut the apples: each child gets
only whole apples. bolish/main.go (“division”):
package main
import "fmt"
func main() {
olma := 7
bola := 2
fmt.Println(olma / bola)
}- Lines 6–7: 7 under
olma(“apple”), 2 underbola(“child”). Both areint. - Line 8: in code,
/(the slash) means division:olma / bolais “apples divided by children”.
What will it print? Hint: an int holds only whole numbers, and the apples are not cut. A) 3,5 · B) 3 ·
C) 4
Answer
B: 3. Both values are int, so the result is a whole number too. Go simply drops the fractional part; it does not
round. Just like in life: each child gets three apples, and one is left over. If you chose “3,5” (three and a half,
written with a comma as in Uzbek schools), you expected ordinary school division. If you chose “4”, you thought Go
rounds 3.5: it does not round, it drops the fraction.
$ go run bolish/main.go
3Go’s official rules call this “truncated division”: when whole numbers are divided, the fractional part is cut off.
A number with a fraction: float64
What if we need three and a half? We pour seven litres of water into two bottles: water can be shared equally.
In code, a fraction is written with a point, not a comma: not 3,5 but 3.5. 7 is a whole number, 7.0 is a
number with a point. In Go, a number with a point is called float64 (a floating-point number).
suv/main.go:
package main
import "fmt"
func main() {
suv := 7.0
shisha := 2.0
fmt.Println(suv / shisha)
}- Lines 6–7:
7.0undersuv(“water”),2.0undershisha(“bottle”). They have a point, so both arefloat64. - Line 8: division just as in
bolish, but now withfloat64values.
What will it print? Guess: A) 3 · B) 3.5 · C) an error
Answer
B: 3.5. The type is float64, so the fractional part was kept. If you chose “3”, you applied the int rule to
float64 too. The type of the result depends on the type of the values.
$ go run suv/main.go
3.5The two numbers side by side:
| Code | Type | Value | Fractional part |
|---|---|---|---|
olma := 7 / 2 | int | 3 | dropped |
suv := 7.0 / 2.0 | float64 | 3.5 | kept |
int and float64 are two different types too. So if you mix int and float64 variables, you get the same
“types don’t match” error. You will see it yourself in the 🔴 homework task.
Going deeper (optional): a number written straight into the code
This section is not in the video; it comes from the course README. Above we said “variables of two different
types”. A number or text written straight into the code (2.0, 15, "Yosh: ") is called a constant. Its type
is not fixed yet, so it is a bit flexible. The Go spec rule: the types must be the same “unless the operation
involves shifts or untyped constants”, that is, there is an exception when an untyped constant takes part.
Three examples (they are not in the course folder; we ran them separately). The first one, konst/main.go:
package main
import "fmt"
func main() {
olma := 7
fmt.Println(olma / 2.0)
}$ go run konst/main.go
3No error: 2.0 was used as the whole number 2, because olma is an int. The fraction was still dropped.
The second one, konst2/main.go: the same program, only 2.5 instead of 2.0:
package main
import "fmt"
func main() {
olma := 7
fmt.Println(olma / 2.5)
}$ go run konst2/main.go
# command-line-arguments
konst2/main.go:7:21: 2.5 (untyped float constant) truncated to int“2.5 (an untyped fractional constant) would be cut off when turned into an int”. 2.5 can’t become a whole number
without losing the fraction, so Go refuses.
The third one, konst3/main.go: both are constants, but one is text and one is a number:
package main
import "fmt"
func main() {
fmt.Println("Yosh: " + 15)
}$ go run konst3/main.go
# command-line-arguments
konst3/main.go:6:14: invalid operation: "Yosh: " + 15 (mismatched types untyped string and untyped int)untyped string is text whose type is not fixed yet, untyped int is a whole number whose type is not fixed yet. The
flexibility works only between numbers: text and numbers still don’t mix. More about types:
GO-1.02 · Types and overflow.
Common mistakes
1. Adding a number to text with plus (the most famous mistake). A string + int such as "Yosh: " + yosh. This
is the mismatched types string and int error from the section “Mixing types: an error”. Fix: turn the number
into text with strconv.Itoa(yosh) and add import "strconv".
2. The number was left in quotes. We want to add 1 to yosh, but we wrote 15 in quotes (matnson/main.go
(“text-number”), not in the course folder):
package main
import "fmt"
func main() {
yosh := "15"
fmt.Println(yosh + 1)
}$ go run matnson/main.go
# command-line-arguments
matnson/main.go:7:14: invalid operation: yosh + 1 (mismatched types string and untyped int)Line 7, character 14 (yosh): “wrong operation: the types don’t match, a string and an untyped whole number”.
"15" is text, like a phone number: you don’t calculate with it. Fix: remove the quotes: yosh := 15.
3. Writing a fraction with a comma. At school in Uzbekistan we write 7,5, but in Go that doesn’t work
(vergul/main.go (“comma”), not in the course folder):
package main
import "fmt"
func main() {
suv := 7,5
fmt.Println(suv)
}$ go run vergul/main.go
# command-line-arguments
vergul/main.go:6:9: assignment mismatch: 1 variable but 2 values“Assignment mismatch: 1 variable but 2 values”. Go read the comma as a separator between two values: 7 and 5. (In
the previous lesson, the comma in fmt.Println(ochko, rekord) also separated two values.) Fix: use a point:
suv := 7.5.
4. Forgetting import "strconv". If we delete line 4 of aylantir/main.go (importsiz/main.go (“without
import”), not in the course folder):
package main
import "fmt"
func main() {
gap := "Yosh: "
yosh := 15
fmt.Println(gap + strconv.Itoa(yosh))
}$ go run importsiz/main.go
# command-line-arguments
importsiz/main.go:8:20: undefined: strconvLine 8, character 20 (strconv): “the name strconv was not found”. The box is not connected, so Go doesn’t know it.
Fix: add the line import "strconv".
5. Expecting 3.5 from 7 / 2 (a silent bug). There is no error message, but the result is 3. We saw this in
the section “Division: why 7 / 2 gives three”: if both values are int, the fraction is dropped. Fix: if you
need the fraction, write numbers with a point: 7.0 / 2.0 (float64).
6. Forgetting the space (a silent bug). "Mening yoshim" + strconv.Itoa(yosh) runs without errors, but the word
and the number stick together (boshjoy/main.go (“space”), not in the course folder; Mening yoshim means “My
age”):
package main
import "fmt"
import "strconv"
func main() {
yosh := 15
fmt.Println("Mening yoshim" + strconv.Itoa(yosh))
}$ go run boshjoy/main.go
Mening yoshim15Plus joins texts without adding anything between them. Fix: put the space at the end of the text:
"Mening yoshim ", just like in "Yosh: ".
Check yourself
Answer first, then open the answer.
1. Which type is "15" in quotes?
Answer
string, because it has quotes. It is not a number but two characters: 1 and 5.
2. What happens if we combine a string and an int with plus?
Answer
The compiler does not translate the program: you get the mismatched types string and int (“the types don’t match”)
error. To fix it, we turn the number into text with strconv.Itoa.
3. If both are int, what is 7 / 2?
Answer
3, because the fractional part is dropped (not rounded). To get 3.5, we write 7.0 / 2.0: those are float64.
4. What does this program print? (ikkiming/main.go (“two thousand”), not in the course folder.)
package main
import "fmt"
func main() {
fmt.Println("2000" + "2000")
fmt.Println(2000 + 2000)
}A) 4000 and 4000 · B) 20002000 and 4000 · C) an error
Answer
B. In the first line two string values were joined; in the second, two int values were added. There is no
error, because each plus has the same type on both sides.
$ go run ikkiming/main.go
20002000
40005. From the previous lesson: what does this program print? And what happens if we write := instead of = in
line 7? (narx/main.go, not in the course folder; narx means “price”.)
package main
import "fmt"
func main() {
narx := "10"
narx = narx + "0"
fmt.Println(narx)
}Answer
It prints 100: let narx now be "10" and "0" joined, that is, the text "100" (not the number one hundred).
$ go run narx/main.go
100With := you get the error no new variables on left side of :=: the label narx was created in line 6, and it
can’t be created a second time (GO-0.02).
Homework
The starter files are in the lesson folder on GitHub: lessons/000-03-son-va-matn . The answers are in the “Uy vazifasi” (Homework) section of the lesson README . Try it yourself first.
🟢 Easy (uyga/oson/main.go): the program prints 7 / 2 as 3. Change only two lines so that it prints 3.5.
package main
import "fmt"
func main() {
olma := 7
bola := 2
fmt.Println(olma / bola)
}🟡 Medium (uyga/orta/main.go): the program prints two lines: Mening yoshim (“My age”) and 15. Print them on
one line as Mening yoshim 15.
package main
import "fmt"
func main() {
yosh := 15
fmt.Println("Mening yoshim")
fmt.Println(yosh)
}🔴 Hard (uyga/qiyin/main.go): try dividing olma := 7 by shisha := 2.0. Read the error and fix it.
package main
import "fmt"
func main() {
olma := 7
shisha := 2.0
fmt.Println(olma / shisha)
}Answer
🟢 Add a point in lines 6–7: olma := 7.0 and bola := 2.0. Now both are float64. (The answer file is not in the
course folder; we ran it as javob-oson/main.go.)
package main
import "fmt"
func main() {
olma := 7.0
bola := 2.0
fmt.Println(olma / bola)
}$ go run javob-oson/main.go
3.5Note: a float64 that comes out whole is printed without a point. fmt.Println(6.0 / 2.0) prints 3, not 3.0.
🟡 Connect strconv and turn the number into text. The space at the end of the text separates the word from the
number:
package main
import "fmt"
import "strconv"
func main() {
yosh := 15
fmt.Println("Mening yoshim " + strconv.Itoa(yosh))
}$ go run javob-orta/main.go
Mening yoshim 15🔴 First, the error:
$ go run uyga/qiyin/main.go
# command-line-arguments
uyga/qiyin/main.go:8:14: invalid operation: olma / shisha (mismatched types int and float64)Line 8, character 14 (olma): “the types don’t match: one is int, one is float64”. int and float64 are
different types too. There are two fixes:
- add a point, as in the video:
olma := 7.0→3.5; - or convert explicitly:
fmt.Println(float64(olma) / shisha).float64(...)turns the whole number in the parentheses into a fractional number. Go never does this silently by itself; you write it.
package main
import "fmt"
func main() {
olma := 7
shisha := 2.0
fmt.Println(float64(olma) / shisha)
}$ go run javob-qiyin/main.go
3.5In the next lesson we compare numbers, and the program makes its own decisions with if:
GO-0.04 · if: the program makes a decision.
Sources
- Go’s official rules (the Go spec): Operators (“the operand types must be
identical unless the operation involves shifts or untyped constants”),
Arithmetic operators (
+for numbers and strings), String concatenation , Integer operators (“truncated division”), Constants (default typesint,float64,string) - strconv.Itoa documentation
- Go FAQ: Why does Go not provide implicit numeric conversions?
- MDN: Addition (+)
(
"2" + 2→"22", an example of a language that converts silently) - go/token.Position (the line and character numbers in an error message)
- Harvard CS50x, Week 1 notes (
7 / 2→3) - All results were produced with
go1.26.8 linux/amd64on 2026-11-26.
Video and code
▶️ The video is coming soon. This page teaches the whole lesson, so you don’t have to wait for it.
💻 Code and homework answers on GitHub
← Previous lesson: GO-0.02 · Next lesson: GO-0.04 → · All lessons
comments