How to cast reflect.Value to its type?
type Cat struct { Age int } cat := reflect.ValueOf(obj) fmt.Println(cat.Type()) // Cat fmt.Println(Cat(cat).Age) // doesn't compile fmt.Println((cat.(Cat)).Age) // same
Thanks!
concreteCat,_ := reflect.ValueOf(cat).Interface().(Cat)
see http://golang.org/doc/articles/laws_of_reflection.html fox example
type MyInt int var x MyInt = 7 v := reflect.ValueOf(x) y := v.Interface().(float64) // y will have type float64. fmt.Println(y)
Ok, I found it
reflect.Value
has a function Interface()
that converts it to interface{}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With