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
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
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 a value to a different type in Go uses the syntax ____, such as float64(i).
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 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, nilVariables & Basic Types
10 questions · pass with 60% — just like in the app.
Which keyword declares a variable with an explicit type in Go?
The := short declaration operator can be used at the package level, outside of any function.
Inside a function, the ____ operator declares a new variable and infers its type in one step.
Which of these is Go's default, recommended floating-point type?
In Go, boolean literals are written lowercase: true and false.
A variable declared with var but no initial value automatically receives a ____ appropriate to its type.
What happens when you add an int and a float64 directly without converting either one?
Converting a value to a different numeric type in Go uses the syntax ____, such as float64(i).
Converting a float64 like 4.7 to an int in Go rounds it to the nearest whole number.
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 →