通过反射获取阴影方法

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

When you embed a struct into another, say A into B, you are only making a field in struct B named A of type A. As a syntactic convenience, you can call methods on A directly on B, but semantically there is no special thing about "shadowed methods"; they are just methods on a type A value with happens to be inside a struct.

You just need to mimick your b.A.Test() with the reflecion API. The bad thing is that reflection doesn't perform the kind of sugar that plain Go does, so the thing you need to mimick is (&((*b).A)).Test().

val.Elem(). // Go to *B
FieldByName("A").     // Find field named A
Addr().               // Take its address, since Test has a method receiver of type *A
MethodByName("Test"). // Find its method Test
Call([]reflect.Value{})

Updated code:

http://play.golang.org/p/67xc66ULFz

(By the way, calling method receivers "self" is not idiomatic in Go.)