← 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.
Syntax
go
targetType(value)

strconv.Itoa(intValue)
strconv.Atoi(stringValue)

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
// Every Go file belongs to a package; main builds an executable
package main

// Import the fmt package
import "fmt"

// main is where the program starts running
func main() {
	// Declare int and set it to 10
	var i int = 10
	// Declare float64 and set it to float64(i) / 3
	var f float64 = float64(i) / 3
	// Print the values followed by a newline
	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
// Every Go file belongs to a package; main builds an executable
package main

// Import the fmt package
import "fmt"

// main is where the program starts running
func main() {
	// Declare int and set it to 300
	var big int = 300
	// Declare int8 and set it to int8(big)
	var small int8 = int8(big)
	// Print the values followed by a newline
	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
// Every Go file belongs to a package; main builds an executable
package main

// Import the packages this program uses
import (
	"fmt"
	"strconv"
)

// main is where the program starts running
func main() {
	// Short declaration: create n, err and infer the type
	n, err := strconv.Atoi("42")
	// Handle the error if one occurred
	if err != nil {
		// Print the values followed by a newline
		fmt.Println("conversion failed:", err)
		return
	}
	// Print the values followed by a newline
	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
// Every Go file belongs to a package; main builds an executable
package main

// Import the packages this program uses
import (
	"fmt"
	"strconv"
)

// main is where the program starts running
func main() {
	// Short declaration: create score and infer the type
	score := 95
	// Short declaration: create text and infer the type
	text := strconv.Itoa(score)
	// Print the values followed by a newline
	fmt.Println("score as text: \"" + text + "\"")
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
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.