← Back to Go Course | Chapter 2: Variables & Types | Lesson 6 of 7

Type Conversion

Type conversion is like pouring juice from one shaped cup into another -- the juice is the same, but you have to pour it on purpose, since Go never does it for you automatically.

Converting Between Numeric Types

Go never implicitly converts between numeric types, even ones that seem compatible like int and float64 -- you must convert explicitly using TargetType(value) syntax. This strictness catches bugs where precision could be silently lost.

Example: Converting Between Numeric Types

markup
package main

import "fmt"

func main() {
	var i int = 10
	var f float64 = float64(i) / 3
	fmt.Println("as float:", f)
}

Truncation with Narrowing Conversions

Converting from a larger numeric type to a smaller one (like int to int8) doesn't raise an error if the value doesn't fit -- it silently truncates or wraps around, which can introduce subtle bugs if the source value is out of range.

Note: Always check whether a value fits before converting to a smaller type, to avoid silent data loss.

Example: Truncation with Narrowing Conversions

markup
package main

import "fmt"

func main() {
	var big int = 300
	var small int8 = int8(big)
	fmt.Println("300 truncated to int8:", small)
}

String to Number with strconv

Converting a string like "42" into an actual number isn't a type conversion in Go's sense -- it requires the strconv package's parsing functions, such as strconv.Atoi, which can fail (e.g. on non-numeric input) and therefore return an error alongside the result.

Note: Always check the error returned by strconv functions before using the parsed value.

Example: String to Number with strconv

markup
package main

import (
	"fmt"
	"strconv"
)

func main() {
	n, err := strconv.Atoi("42")
	if err != nil {
		fmt.Println("conversion failed:", err)
		return
	}
	fmt.Println("parsed number:", n+8)
}

Number to String

Going the other way, converting a number into its textual representation also uses strconv, typically strconv.Itoa for ints, rather than a plain string(n) conversion, which would misleadingly convert the number to a single Unicode character instead.

Example: Number to String

markup
package main

import (
	"fmt"
	"strconv"
)

func main() {
	score := 95
	text := strconv.Itoa(score)
	fmt.Println("score as text: \"" + text + "\"")
}
Common Mistakes
  1. Expecting Go to automatically convert an int to a float64 in mixed arithmetic, when it actually requires an explicit conversion.
  2. Converting a large int64 down to int8 and being surprised by silently wrapped/truncated values instead of an error.
  3. Using strconv functions incorrectly, like passing a non-numeric string to strconv.Atoi and ignoring the returned error.
Chapter Summary
  • Go requires explicit conversion between different numeric types, even between int and float64.
  • Conversion syntax is TargetType(value), such as float64(x) or int(y).
  • Converting between string and numeric types requires the strconv package, not a plain type conversion.
  • Narrowing conversions (e.g. int64 to int8) can silently truncate data, so use them carefully.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.