package main import ( "fmt" "reflect" ) func main() { var x float64 = 12345678901234567890 v := reflect.ValueOf(x) _, err := v.Convert(reflect.TypeOf(int(0))) if err != nil { fmt.Println("Overflow error") } }
package main import ( "fmt" "reflect" ) func main() { var x int32 = -2147483648 v := reflect.ValueOf(x) _, err := v.Convert(reflect.TypeOf(uint32(0))) if err != nil { fmt.Println("Overflow error") } }In this example, we create an int32 variable with the minimum value that can be represented by this data type. We again use the ValueOf() function to get a Value object representing the variable and try to convert it to a uint32. The OverflowInt() method is called internally and detects that the value is too large to be converted without loss of precision, resulting in an overflow error. The Reflect package is the package library used in these examples.