package main import ( "fmt" "reflect" ) func main() { var num float64 = 3.14159 value := reflect.ValueOf(num) fmt.Println("Value of num:", value.Float()) }
package main import ( "fmt" "reflect" ) func main() { var num float64 = 3.14159 value := reflect.ValueOf(&num) element := value.Elem() element.SetFloat(2.71828) fmt.Println("New value of num:", num) }This example demonstrates how to modify the value of a float variable using Go Reflect. The reflect.ValueOf function is used to create a new Value instance from a pointer to the variable. The Elem method is then called on the value to get the actual value of the variable. The SetFloat method is then called on the element to change the value of the variable. Finally, we print the new value of the variable to the console.