package main import ( "fmt" "reflect" ) func main() { x := 42 v := reflect.ValueOf(x) fmt.Println(v.Int()) // prints 42 }
package main import ( "fmt" "reflect" ) type person struct { name string age int } func main() { p := person{name: "Alice", age: 30} v := reflect.ValueOf(p.age) fmt.Println(v.Int()) // prints 30 }In this example, we have a struct type person with name and age fields. We create an instance of person and call reflect.ValueOf on the age field. We then call the Int method to return the int value of p.age. The output will be 30. Both of these examples use the reflect package in Go to access the int value of a reflect.Value.