Go Reflection with Embedded Interface in Struct
Detecting "Real" Functions
Go's reflection package provides access to information about types and values at runtime. This can be a powerful tool, but it can also be confusing when working with embedded interfaces in structs.
Consider the following example:
type A interface { Foo() string } type B struct { A bar string }
As you've observed, Go does not enforce the implementation of the embedded interface at compile-time. This means it's possible to embed an interface in a struct without providing an implementation.
Using Reflection to Access Methods
You can use the reflection package to get methods from a struct's embedded interface, even if no implementation is provided:
bType := reflect.TypeOf(B{}) bMeth, has := bType.MethodByName("Foo") if has { fmt.Printf("HAS IT: %s\n", bMeth.Type.Kind()) res := bMeth.Func.Call([]reflect.Value{reflect.ValueOf(B{})}) val := res[0].Interface() fmt.Println(val) } else { fmt.Println("DOESNT HAS IT") }
However, if the embedded interface has no implementation, this code can panic.
Detecting the Absence of an Implementation
To detect if an embedded interface has no implementation, you can check the pointer to the function in the function table of the anonymous interface value:
b := B{} bType := reflect.TypeOf(b) bMeth, has := bType.MethodByName("Foo") if has { bMethPtr := bMeth.Func.Pointer() if bMethPtr == 0 { fmt.Println("No implementation") } else { fmt.Println("Implementation found") } } else { fmt.Println("Interface not embedded") }
If the pointer to the function is 0, there is no implementation. Otherwise, an implementation exists.
Alternative Approach
You can also use a more simplistic approach:
if b.A != nil { b.Foo() }
If the embedded interface is set to nil, the b.Foo() call will panic. Otherwise, it will execute the implementation (if any).
The above is the detailed content of How Can I Reliably Detect If an Embedded Interface in a Go Struct Has a Function Implementation Using Reflection?. For more information, please follow other related articles on the PHP Chinese website!