Get shadowed method via reflection

Answer #1 100 %

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

You’ll also like:


© 2023 CodeForDev.com -