Type Conversions & Assertions

A conversion T(v) changes a value's type at compile time while a type assertion x.(T) extracts the concrete type stored inside an interface at run time — two distinct tools Go keeps explicit and safe.

Learn Type Conversions & Assertions in our free Go course — an interactive lesson with runnable examples, a practice exercise and a quick reference.

Part of the free Go course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

What You'll Learn in This Lesson

1️⃣ Explicit Conversions: T(v)

Go performs no automatic numeric conversion — you must write it out: float64(i) , int(x) , uint8(i) . Converting a float to an int truncates toward zero (no rounding). Conversions between string and []byte / []rune are also built in. Watch the special case: string(65) gives the character "A" , not the text "65" .

2️⃣ strconv: Strings ↔ Numbers

To turn text into numbers (and back), use the strconv package — a plain conversion won't parse digits. Atoi reads a string into an int , Itoa does the reverse, and ParseFloat reads a float64 . Crucially, these return an error on bad input rather than panicking — so you check err and handle it.

3️⃣ Type Assertions & Type Switches

When a value is held in an interface (like interface{' '} ), you recover its real type with an assertion x.(T) . The single-result form panics on a mismatch, so prefer the comma-ok form v, ok := x.(T) , which sets ok to false instead. To handle several possibilities at once, a type switch ( switch x := v.(type) ) branches on the dynamic type cleanly.

🎯 Your Turn

Both numbers arrive as text. Fill in the blanks to parse them with strconv.Atoi , then add them.

Write kind(v interface{' '}) string with a type switch that returns "int" , "string" , "bool" , or "other" , then loop over vals and print each value with its kind.

Practice quiz

How do you convert an int i to a float64?

  • i.(float64)
  • (float64)i
  • float64(i)
  • i as float64

Answer: float64(i). Go uses the target type as a function: float64(i).

Does Go convert numeric types automatically?

  • No, every numeric conversion is explicit
  • Yes, ints widen to floats
  • Yes, but only in arithmetic
  • Only for untyped constants in vars

Answer: No, every numeric conversion is explicit. Go has no implicit numeric conversion; you must write it out.

What does int(x) do to the float64 9.99?

  • Rounds to 10
  • Rounds to nearest even
  • Returns an error
  • Truncates toward zero to 9

Answer: Truncates toward zero to 9. Float-to-int truncates; use math.Round first if you need rounding.

What does string(rune(72)) produce?

  • "72"
  • "H"
  • 72
  • "r"

Answer: "H". Converting a code point to string yields that character; 72 is 'H'.

Which call turns the string "42" into the int 42?

  • strconv.Atoi("42")
  • int("42")
  • strconv.Itoa("42")
  • string(42)

Answer: strconv.Atoi("42"). Atoi parses a string to int; Itoa does int to string.

What does strconv.Itoa(2024) return?

  • The int 2024
  • The float 2024.0
  • The string "2024"
  • An error

Answer: The string "2024". Itoa converts an int to its decimal string.

What does strconv.Atoi do on bad input like "oops"?

  • Panics
  • Returns 0 and a non-nil error
  • Returns -1
  • Returns the empty string

Answer: Returns 0 and a non-nil error. strconv functions report an error instead of panicking.

Which is the safe (comma-ok) type assertion form?

  • v := x.(T)
  • v := x.(type)
  • v := assert(x, T)
  • v, ok := x.(T)

Answer: v, ok := x.(T). The comma-ok form sets ok to false on mismatch instead of panicking.

What does a bare x.(T) do if x does not hold type T?

  • Returns nil
  • Panics
  • Returns the zero value silently
  • Compile error

Answer: Panics. The single-result assertion panics on a type mismatch.

Which construct branches on the dynamic type of an interface value?

  • switch v { ... }
  • if v is T { ... }
  • switch x := v.(type) { ... }
  • typeof(v)

Answer: switch x := v.(type) { ... }. A type switch reads v.(type) and branches per concrete type.