Type Conversions in Go
Explicit conversions, type aliases and defined types
Why Explicit Conversions?
Go never implicitly converts between types. This prevents subtle bugs where a small integer silently overflows a larger one or a float loses precision.
- Conversions must be written explicitly:
T(value) - The compiler catches mismatched types at compile time
- This makes Go code predictable and safe
Numeric Conversions
Convert between numeric types with the target type as a function call:
package main
func main() {
var i int = 42
var f float64 = float64(i)
var u uint = uint(f)
_ = u
}All lessons in this course
- Declaring Variables in Go
- Go's Built-in Types
- Constants and iota
- Type Conversions in Go