100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Go

Type Conversion in Go

Learn why Go requires explicit type conversion between numeric and other types, and how to perform it safely.

BasicsBeginner8 min readJul 8, 2026
Analogies

Introduction

Go is strict about type safety: it never implicitly converts between different numeric types, even between closely related ones like int and float64. Every conversion must be explicit, written as TypeName(value). This design choice prevents subtle bugs caused by silent, unintended conversions that can happen in more permissive languages.

🏏

Cricket analogy: A scorer never silently converts a boundary count into strike rate without recalculating explicitly, since blending overs and runs implicitly would misreport the innings, just as Go refuses silent numeric conversions.

Syntax

go
var i int = 42
var f float64 = float64(i)
var u uint = uint(f)

// string <-> numeric conversions use strconv, not direct casting
// float64(x), int(x), string(x) are direct type conversions only
// for compatible underlying representations

Explanation

To convert a value from one numeric type to another, you call the target type as if it were a function: float64(i) converts int i to a float64. This works between numeric types (int, float32, float64, etc.) and also between compatible types like rune and int32. A common gotcha for beginners coming from languages like Python or JavaScript is expecting Go to automatically promote an int to a float64 in a mixed expression; Go will refuse to compile such code, requiring an explicit conversion instead. Converting between numbers and strings is different: converting an int directly to string, like string(65), converts by Unicode code point (producing "A"), not the digit characters — for that, use the strconv package's functions like strconv.Itoa and strconv.Atoa (Atoi). Conversions can also lose precision or overflow, such as converting a large float64 to int, which truncates the decimal part.

🏏

Cricket analogy: Converting a bowler's economy rate to a whole number by calling round(economy) is like float64(i) - deliberate and explicit - while assuming a Duckworth-Lewis target auto-adjusts without recalculation is the beginner mistake Go prevents.

Example

go
package main

import (
	"fmt"
	"strconv"
)

func main() {
	var a int = 10
	var b float64 = 3.0

	// result := a / b // compile error: mismatched types
	result := float64(a) / b
	fmt.Println("result:", result)

	num, err := strconv.Atoi("123")
	if err != nil {
		fmt.Println("conversion error:", err)
	}
	fmt.Println("parsed:", num)
}

Output

go
result: 3.3333333333333335
parsed: 123

Key Takeaways

  • Go never performs implicit type conversion, even between related numeric types.
  • Explicit conversion syntax is TargetType(value), e.g. float64(x).
  • Mixing int and float64 directly in an expression is a compile-time error.
  • Use the strconv package (Atoi, Itoa, ParseFloat, etc.) to convert between strings and numbers.
  • Converting a float to an int truncates the decimal portion, potentially losing precision.

Practice what you learned

Was this page helpful?

Topics covered

#Go#GoProgrammingStudyNotes#Programming#TypeConversionInGo#Type#Conversion#Syntax#Explanation#StudyNotes#SkillVeris