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
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 representationsExplanation
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
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
result: 3.3333333333333335
parsed: 123Key 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
1. What happens if you try to divide an int by a float64 directly in Go without conversion?
2. What does string(65) evaluate to in Go?
3. Which package should you use to convert the string "123" into the integer 123?
4. What is the correct syntax for explicitly converting an int variable x to float64?
5. What happens when converting a float64 value like 3.9 to int?
Was this page helpful?
You May Also Like
Variables and Data Types in Go
Learn how Go declares variables with var and :=, and the built-in types like int, float64, bool, and string.
Operators in Go
Explore arithmetic, comparison, logical, and bitwise operators in Go, including the unique AND NOT operator.
Input and Output in Go
Learn to print formatted output with fmt.Printf and read user input using fmt.Scanln and bufio.Reader.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics