Swipe
SwipeSwipe
Swipe Apps

Paths

Motivation

Information

Variables & Basic Types · Lesson 9 of 9

Type Conversion

A free lesson from Swipe Coding · Go, level 2 · updated October 2026

By the Swipe team, founded by Colin · how we make lessons

🤖
This is one of the strictest, most deliberate rules in the whole language. 🐹

Unlike many languages, Go never automatically converts between numeric types, even closely related ones like int and float64. Mixing them directly in an expression is a compile error — Go refuses to guess which type you actually meant, forcing you to be explicit instead.

var i int = 10
var f float64 = 3.5

// result := i + f // compile error: mismatched types int and float64
Adding an int and a float64 directly fails to compile — Go will not silently convert either one for you.
Quick check

What happens when you try to add an int and a float64 directly in Go, without converting either one?

var i int = 10
var f float64 = 3.5

result := i + f

To fix it, you convert explicitly using the syntax TargetType(value) — it looks like a function call, but it is Go's built-in type conversion. float64(i) converts i to a float64, and int(f) would convert f to an int.

var i int = 10
var f float64 = 3.5

result := float64(i) + f
fmt.Println(result) // 13.5
Converting i to a float64 first makes both operands the same type, so the addition compiles and produces 13.5.
Fill in the blank

Converting a value to a different type in Go uses the syntax ____, such as float64(i).

🤖
Round versus truncate is a classic source of off-by-one surprises — good to nail down early. ✂️

Converting a float64 to an int truncates the decimal part — it does not round. int(3.9) produces 3, not 4, because Go simply discards everything after the decimal point rather than rounding to the nearest whole number.

x := 3.9
y := int(x)
fmt.Println(y) // 3, not 4 — it truncates, it does not round
Converting 3.9 to an int drops the .9 entirely, leaving 3 — Go never rounds during a numeric conversion.
True or false

Converting a float64 like 3.9 to an int in Go rounds it to the nearest whole number.

x := 3.9
y := int(x)

Converting between numbers and strings isn't done with this same TargetType(value) syntax — that only works between numeric types. For that, you use the strconv package instead: strconv.Itoa(42) turns an int into the string "42", and strconv.Atoi("42") turns a string back into an int, along with an error value you will learn to handle a bit later.

import "strconv"

s := strconv.Itoa(42)        // "42"
n, err := strconv.Atoi("42") // 42, nil
strconv.Itoa converts an int to a string, and strconv.Atoi converts a string back to an int, returning an error alongside the result.
Chapter quiz

Variables & Basic Types

10 questions · pass with 60% — just like in the app.

Quick check

Which keyword declares a variable with an explicit type in Go?

True or false

The := short declaration operator can be used at the package level, outside of any function.

Fill in the blank

Inside a function, the ____ operator declares a new variable and infers its type in one step.

Quick check

Which of these is Go's default, recommended floating-point type?

True or false

In Go, boolean literals are written lowercase: true and false.

Fill in the blank

A variable declared with var but no initial value automatically receives a ____ appropriate to its type.

Quick check

What happens when you add an int and a float64 directly without converting either one?

Fill in the blank

Converting a value to a different numeric type in Go uses the syntax ____, such as float64(i).

True or false

Converting a float64 like 4.7 to an int in Go rounds it to the nearest whole number.

Quick check

Which package provides functions like Itoa and Atoi for converting between strings and numbers?

Keep your streak going in Swipe Coding

Swipe through every level of Go on your iPhone — with XP, stars, streaks, a personal advisor and the full track beyond these free levels.

Get the Swipe apps →