How can I get the shadowed methods via reflection?
In the code below I'm using MethodByName to get method Test() on type B, but I would like to get the shadowed method Test() from b.A as well, so I can call both of them.
package main
import (
"fmt"
"reflect"
)
type A struct {}
type B struct {
A
}
func (self *A) Test() {
fmt.Println("I'm A")
}
func (self *B) Test() {
fmt.Println("I'm B")
}
func main() {
b := &B{}
b.Test() // This one shadows b.A.Test()
b.A.Test() // but its ok to call like this
// via reflection
val := reflect.ValueOf(b)
val.MethodByName("Test").Call([]reflect.Value{})
}
Code available here: http://play.golang.org/p/6YPLy2dmMb
If you have an embedded struct and shadowing, you can always get the field as if it were a member variable with the same name as the type of the embedded struct, this is shown by your line b.A.Test().
So what do we do? Use reflect.Value.FieldByName to get the field. With your exact setup it's a bit janky. You can't use FieldByName on a pointer to a struct.
package main
import (
"fmt"
"reflect"
)
type A struct{}
type B struct {
A
}
func (self *A) Test() {
fmt.Println("I'm A")
}
func (self *B) Test() {
fmt.Println("I'm B")
}
func main() {
b := &B{}
b.Test()
b.A.Test()
val := reflect.ValueOf(b)
subVal := val.Elem().FieldByName("A").Addr()
subVal.MethodByName("Test").Call([]reflect.Value{})
val.MethodByName("Test").Call([]reflect.Value{})
}
As seen, it's a bit ugly. You first need to call Elem to get the value val points to, then get the field, then get the pointer to the field because A.Test is actually on (*A), and not A. While Go pointers are normally transparent, it doesn't apply to reflection, unfortunately, so you have to do all explicit addressing/dereferencing yourself, but if you understand pointers it's pretty straightforward.
Edit: Playground link to the code above
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