JavaRush /Courses /Go SELF /Diving deeper into the basic types bool, string, int, flo...

Diving deeper into the basic types bool, string, int, float64

Go SELF
Level 6, Lesson 2
Available

1. Reviewing everything we already know

When you write a program, you constantly make deals. You promise the compiler: “this variable will store an integer,” and the compiler promises you: “then I will validate operations so you don’t accidentally add a cucumber to a temperature.” A type in Go is a contract: which values are valid and which operations are allowed.

In Go (as in most statically typed languages), a variable’s type is fixed at the moment of declaration and does not “change its shoes” as the program runs. That is why it is very useful to learn to predict in advance: “is this expression an int or a float64?”, “will a comparison produce a bool?”, “can strings be multiplied?” (spoiler: they cannot).

2. int: integers and ordinary arithmetic

int is the main workhorse type for integers: number of tasks, age, number of attempts, position in a loop, counters, sums without fractions. It is convenient because it supports familiar arithmetic and comparison operations. But it has a personality: division for int is integer division, and it discards the fractional part without remorse.

Basic operations for int

For int, the operators +, -, *, /, % (remainder), as well as comparisons == != < <= > >= are available.

package main

import "fmt"

func main() {
	a := 10
	b := 3

	fmt.Println(a / b) // 3
	fmt.Println(a % b) // 1
}

What matters here is the behavior of /: 10 / 3 yields 3, not 3.333.... The fractional part “disappears,” and this is not a bug, but a rule.

Operator precedence

Usually *, /, % are evaluated before + and -. If you want “add first, then multiply,” use parentheses—it is not a sign of weakness, it is a sign of readable code.

package main

import "fmt"

func main() {
	x := 2 + 3*4
	y := (2 + 3) * 4

	fmt.Println(x) // 14
	fmt.Println(y) // 20
}

3. float64: numbers with a fractional part

float64 is the basic type for real numbers (with a fractional part). Percentages, averages, measurements, “how many hours are left,” “how many kilometers,” “temperature”—all of these often belong in float64. In Go, decimal literals (for example, 19.99) have type float64 by default, and it is useful to remember this because that is exactly how type inference works with :=.

It is important to understand that float64 is not “just a number,” but a number with storage and rounding quirks. For now, we will keep it simple and focus on operations and comparisons.

Arithmetic for float64

float64 supports +, -, *, / and comparisons. But there is no % for float64—the remainder operator in Go is defined only for integer types.

package main

import "fmt"

func main() {
	price := 19.99
	qty := 2.0

	total := price * qty
	fmt.Println(total) // 39.98 (sometimes it may look like 39.979999..., this is a normal float quirk)
}

Comparisons by type work as well:

package main

import "fmt"

func main() {
	x := 0.5
	fmt.Println(x >= 0.0) // true
}

4. string: text, concatenation, and comparison

string in Go is text. It is often used for names, commands, user messages, “task title,” “status,” “login.” From a beginner’s perspective, it helps to remember two things: strings can be concatenated using +, and compared using == and !=. That is it: attempts to “divide a string by a string” are best left to science fiction.

Concatenation with + and printing

package main

import "fmt"

func main() {
	first := "Go"
	second := "lang"

	full := first + second
	fmt.Println(full) // Golang
}

If you really want a space—that is also a string:

package main

import "fmt"

func main() {
	a := "Hello"
	b := "world"
	fmt.Println(a + " " + b) // Hello world
}

String comparison

package main

import "fmt"

func main() {
	cmd := "add"

	fmt.Println(cmd == "add")    // true
	fmt.Println(cmd != "delete") // true
}

String comparison returns bool, which means it is suitable for if conditions.

5. bool: true/false, comparisons, and conditions

The bool type stores one of two values: true or false. It is used to control branching and logic: bool decides which branch of if runs, when a loop stops, and whether an operation can be considered successful.

The most common “birthplace” of bool is a comparison. Compare two numbers—get a bool. Compare two strings—get a bool. Then that bool can be combined with logical operators &&, ||, !.

bool from a comparison

package main

import "fmt"

func main() {
	age := 20
	isAdult := age >= 18

	fmt.Println(isAdult) // true
}

“And/or/not” logic in a real condition

package main

import "fmt"

func main() {
	age := 20
	hasTicket := true

	canEnter := age >= 18 && hasTicket
	fmt.Println(canEnter) // true
}

Comparisons: a universal way to get bool

The result of any comparison in Go has type bool. It is not “0 or 1,” not “empty/non-empty,” but exactly true/false.

Examples:

package main

import "fmt"

func main() {
	// int
	a := 10
	b := 20
	fmt.Println(a < b)  // true
	fmt.Println(a == b) // false

	// float64
	x := 1.5
	y := 1.0
	fmt.Println(x > y) // true

	// string
	user := "admin"
	fmt.Println(user == "admin") // true
}

6. Why you cannot do int + float64

At this point, you usually get the feeling: “but it’s a number and a number—come on, Go.” But static typing is a seat belt: it is annoying at first, then it saves you from weird bugs.

Go does not allow automatically mixing different numeric types in expressions. You cannot just add an int and a float64. This is a deliberate language rule: operations that mix numeric types are forbidden unless the programmer makes an explicit choice.

Let’s look at a typical example:

package main

import "fmt"

func main() {
	count := 3     // int
	price := 19.99 // float64 (default for a decimal literal)

	fmt.Println(count) // 3
	fmt.Println(price) // 19.99

	total := count * price // will NOT compile: different types (int and float64)
	fmt.Println(total)
}

Why does this matter? Because you should learn to spot the problem in advance. If you have “quantity” and “price,” either do everything in float64, or everything in int (for example, store price in cents). And conversions are a separate, deliberate step—not “automatic in the background.”

7. Cheat sheet: basic types and operations

To avoid holding everything in your head as a fog, it helps to have a small cheat-sheet table. It is not a replacement for understanding, but a map of the terrain so you can orient yourself faster.

Type Example values Main operations Comparisons
int
0, -5, 42
+ - * / %
== != < <= > >=
float64
0.0, 3.14, -1.5
+ - * /
== != < <= > >=
string
"Go", "", "hi"
+ (concatenation) == != (in practice, equality/inequality is usually what you need)
bool
true, false
&& || !
== !=

8. Example: tracking progress on a task

Let’s assemble a small example that uses all four types the way it typically happens in real code: a string is the task name, an integer is the plan, float64 is “how many hours you spent,” and bool is “is the task done?”. This will be a simple console program, without arrays and functions, but as clear as possible.

Read input and make a comparison

package main

import (
	"fmt"
)

func main() {
	var title string
	var planned int
	var spent float64
	var done bool

	fmt.Scan(&title, &planned, &spent, &done)

	isBig := planned >= 5
	fmt.Println(title, isBig, done)
}

How to use it: enter, for example, like this (separated by spaces): report 7 2.5 false

The program will print something like: report true false

Here we saw several things at once: title is a string, planned is an integer, spent is a fraction, done is boolean, and planned >= 5 is a comparison that yields bool.

Add simple “show status” logic

package main

import "fmt"

func main() {
	var title string
	var planned int
	var done bool

	fmt.Scan(&title, &planned, &done)

	if done == true {
		fmt.Println(title + ": DONE")
	} else {
		fmt.Println(title + ": IN PROGRESS")
	}
}

Here we do it in the most straightforward way so that you can see that a comparison yields bool and how a string is concatenated using +. Yes, we could write if done { ... }, but sometimes it helps beginners to see an explicit comparison a couple of times so the brain stops looking for “magic zeros and ones.”

9. Common mistakes

Mistake #1: expecting int division to produce a fraction.
A very common story: someone writes 7 / 2, hopes for 3.5, and gets 3. Nothing “broke”—this is the normal rule for int: division drops the fractional part. If you truly need a fraction, you are using the wrong type. But it is important not to “fix” it with random hacks—first learn to notice that the expression is integer arithmetic.

Mistake #2: trying to use a number or a string as an if condition.
In some languages, “0 is false and everything else is true.” In Go, that is not allowed: the condition in if must be of type bool. So instead of if x { ... } you need to write a comparison, for example if x > 0 { ... }, to get a boolean result.

Mistake #3: mixing int and float64 and expecting the compiler to figure it out.
Go intentionally does not do implicit conversions between numeric types. This protects you from unexpected precision loss and weird effects. When you see int and float64 in the same expression, treat it as a red flag: “I must explicitly decide which type is the main one here.”

Mistake #4: thinking that string supports mathematical operations.
Strings can be concatenated using +, but you cannot divide, multiply, or take a remainder. If you need to “repeat a string 10 times” or “parse a number from a string,” those are separate tasks with separate tools—not arithmetic.

Mistake #5: mixing up where bool comes from.
Beginners sometimes think bool is “some special mode.” In practice, bool most often appears as the result of a comparison (a > b) or a logical expression (x > 0 && ok). If you keep this in mind, conditions stop looking like magic and start reading like ordinary phrases.

1
Task
Go SELF, level 6, lesson 2
Locked
Operation Calculator
Operation Calculator
1
Task
Go SELF, level 6, lesson 2
Locked
Security Post
Security Post
1
Task
Go SELF, level 6, lesson 2
Locked
Stability sensors
Stability sensors
1
Task
Go SELF, level 6, lesson 2
Locked
Task status
Task status
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION