Type casting in Go means explicitly converting a value from one compatible type to another.
newValue := targetType(value)
This is needed because Go is strict about types and does not allow automatic conversions. This helps prevent hidden bugs, makes code behavior predictable, and forces developers to be explicit about how values of different types should interact.
a := 5.6 // float64b := 10 // intc := a + b // invalid operation: a + b (mismatched types float64 and int)c := a + float64(b) // 15.6 (float64)
Casting from float to integer removes (cuts off) the decimal part:
a := 5.8 // float64b := int16(a) // 5 (int16)
Casting between different integer sizes must also be explicit:
var a int64 = 100var b int32 = 50c := int32(a) + b // 150 (int32)
Some types cannot be cast directly if they are not compatible with each other:
a := "123"b := int(a) // cannot convert a (variable of type string) to type int