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 (

type A struct{}
type B struct {

func (self *A) Test() {
    fmt.Println("I'm A")
func (self *B) Test() {
    fmt.Println("I'm B")

func main() {
    b := &B{}

    val := reflect.ValueOf(b)
    subVal := val.Elem().FieldByName("A").Addr()

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 -