Type Conversion
In this page:
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
package main
import "fmt"
func main() {
var i int = 10
var f float64 = float64(i) / 3
fmt.Println("as float:", f)
}
Login to try C/C++/Java/PHP code in the editor
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
package main
import "fmt"
func main() {
var big int = 300
var small int8 = int8(big)
fmt.Println("300 truncated to int8:", small)
}
Login to try C/C++/Java/PHP code in the editor
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
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)
}
Login to try C/C++/Java/PHP code in the editor
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
package main
import (
"fmt"
"strconv"
)
func main() {
score := 95
text := strconv.Itoa(score)
fmt.Println("score as text: \"" + text + "\"")
}
Login to try C/C++/Java/PHP code in the editor
- Expecting Go to automatically convert an int to a float64 in mixed arithmetic, when it actually requires an explicit conversion.
- Converting a large int64 down to int8 and being surprised by silently wrapped/truncated values instead of an error.
- Using strconv functions incorrectly, like passing a non-numeric string to strconv.Atoi and ignoring the returned error.
- 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: