Go Type Casting – Convert Between Data Types Safely in Go (2025 Guide)
Introduction – What Is Type Casting in Go?
In Go, type casting (also known as type conversion) is the explicit process of converting a value from one data type to another. Go does not support implicit type conversion, making type safety a cornerstone of its design.
In this section, you’ll learn:
- How to convert between integers, floats, strings, and booleans
- Syntax and rules for safe type casting
- Common pitfalls and how to avoid them
- Real-world examples with outputs
Basic Type Conversion Syntax in Go
Go uses a function-style syntax for casting:
var a int = 42
var b float64 = float64(a) // Convert int to float64
You specify the target type and wrap the value inside it.
Numeric Type Conversions
Integer to Float
var i int = 10
var f float64 = float64(i)
fmt.Println(f) // Output: 10.0
Float to Integer (Truncates)
var f float64 = 9.81
var i int = int(f)
fmt.Println(i) // Output: 9
The fractional part is discarded, not rounded.
Integer to Byte
var i int = 65
b := byte(i)
fmt.Println(b) // Output: 65
fmt.Println(string(b)) // Output: A
String Conversions
Go does not allow direct string ↔ numeric conversions without helper functions.
This is invalid:
var s string = string(123) // Not "123"
This will output the Unicode character for code point 123 ({).
Use strconv package for safe conversions:
import (
"fmt"
"strconv"
)
func main() {
num := 100
str := strconv.Itoa(num) // int to string
fmt.Println(str) // Output: "100"
s := "42"
i, _ := strconv.Atoi(s) // string to int
fmt.Println(i) // Output: 42
}
Boolean Conversions
Go does not support automatic boolean-to-integer or vice versa conversions.
// Invalid: var i int = int(true)
Instead, use control logic:
var b bool = true
var i int
if b {
i = 1
} else {
i = 0
}
Real-World Example – Type Casting Combo
package main
import (
"fmt"
"strconv"
)
func main() {
var score int = 87
var percent float64 = float64(score) / 100
str := strconv.Itoa(score)
fmt.Printf("Score: %d\n", score)
fmt.Printf("Percentage: %.2f\n", percent)
fmt.Printf("Score (as string): %s\n", str)
}
Output:
Score: 87
Percentage: 0.87
Score (as string): 87
Pitfalls of Type Casting in Go
| Pitfall | Solution |
|---|---|
| Loss of precision (float→int) | Manually round or handle fractions |
string(123) ≠ "123" | Use strconv.Itoa() instead |
| No implicit casts | Always use explicit type conversion |
Panic on invalid Atoi | Use error handling with Atoi |
Summary – Recap & Next Steps
Type casting in Go is explicit, strict, and safe. This prevents many bugs common in loosely typed languages and ensures your data stays valid throughout your program.
Key Takeaways:
- Go requires explicit type conversion between types
- Use function-style casts for numeric types
- Use
strconvpackage to convert strings ↔ numbers - No boolean ↔ numeric casting allowed
- Watch out for truncation and data loss
Next: Learn about Go Operators, including arithmetic, relational, logical, and bitwise operators.
FAQs – Go Type Casting
Does Go support implicit type conversion?
No. Go requires explicit type conversion for safety and clarity.
How do I convert string to int or float in Go?
Use strconv.Atoi() for string to int, and strconv.ParseFloat() for string to float.
Can I convert boolean to integer in Go?
No direct conversion. Use if statements to assign 1 for true, 0 for false.
What happens when I convert a float to int?
The decimal part is discarded (truncated), not rounded.
Why does string(65) return "A" instead of "65"?
Because string(65) converts 65 to its Unicode character, not its string digits. Use strconv.Itoa() instead.
Share Now :
