The situation that I have now is the same as in this topic: The value of a structure with a built-in anonymous interface?
type A interface { Foo() string } type B struct { A bar string }
Idiomatically, based on the background image in OOP languages, what looks like this template, “trying to tell” me that B should implement the interface A. But I still understand that “Go is different”. Thus, rather than checking the compilation time that I expected at the beginning, it will happily compile with or without
func (B) Foo() string { .... }
is present. As mentioned above, the question (rephrased) is: "using built-in interfaces in structures is great when you only want to implement / part / interfaces."
Presumably, this is due to the fact that what happens with this embedding is exactly the same as in any other case - a value of type B will have the value of an anonymous interface of type A as a field. Personally, when I find that orthogonality is calming, I am also confused that the reflection package will then allow me to get methods A directly from type B in this way, and not error / no if there is no method with receiver B. But, this question It’s not connected with thinking, but in how this interface value is initialized after b := B{} :
func main() { 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") } }
When this is done, it causes a terrible panic
HAS IT: func panic: runtime error: invalid memory address or nil pointer dereference
... or not - depending on whether the compiler / runtime could find the above method. So: How can I detect this situation before it starts?
That is - is there something about the bMeth value that I can use to see that there is no real implementation in the returned Method and func return values? More precisely, is it something like "is a pointer to a function in the table of functions of the anonymous value of the interface to zero" or what exactly happens to the methods that you extract from the interface with reflection, where there is no implementation?
Giving the whole thing to goroutine and trying to run the function during deferral / panic is not the answer - not only because of the expense of panic / deferral, but also because the function can, if it exists, have side effects that I don’t want directly now...
Do I need something like a runtime implementation that reflects compiler type checking? Or is there an easier way? Am I thinking about it wrong?
Example above on the Go Playground