title: Go reflection with interface embedded in struct - how to detect "real" functions?
body: <p>The situation I have now is the same as was asked about in this thread:  <a href="https://stackoverflow.com/questions/24537443/meaning-of-a-struct-with-embedded-anonymous-interface">Meaning of a struct with embedded anonymous interface?</a> </p>
  
  <pre><code>  type A interface {
       Foo() string
    }
  
    type B struct {
       A
       bar string
    }
  </code></pre>
  
  <p>Idiomatically, coming from a backround in OOP languages, what it looks like this pattern is "trying to say" to me is that B must implement interface A.   But I get by now that "Go is different".  So, rather than the compile-time check I expected at first, this is happy to compile with or without a</p>
  
  <pre><code>  func (B) Foo() string { .... }
  </code></pre>
  
  <p>present.  As the above question points out (paraphrased): "using embedded interfaces in structs is great for when you only want to implement /part/ of an interface".  </p>
  
  <p>Presumably, this is because what is happening with this embed is just like in every other case - a value of type B would have an anonymous interface value of type A, as a field.  Personally while I find that orthogonality comforting, I also find it confusing that the reflection package would then let me get methods of A directly from B's type this way, and not error/nil if no method with receiver B is present.  But - this question isn't about the thinking behind that - it is about how that interface value is initialized after <code>b := B{}</code>:</p>
  
  <pre><code> 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")
    }
  }
  </code></pre>
  
  <p>When this is run, it causes a horrible panic </p>
  
  <pre><code> HAS IT: func
   panic: runtime error: invalid memory address or nil pointer dereference
  </code></pre>
  
  <p>... <em>or doesn't</em> - depending on if the compiler/runtime was able to find the above method.  So:  <strong>How can I detect that situation before I trigger it?</strong>  </p>
  
  <p>That is - is there something about the bMeth value I can use to see that there is no "real" implementation present in the reflection-returned returned Method and func values?  Is that more precisely something like "is the pointer to the function in the function table of the anonymous interface value in zero", or what exactly is going on with methods you pull from an interface with reflection where there is no implementation?  </p>
  
  <p>Wrapping the whole thing in a goroutine and attempting to run the function under defer/panic isn't the answer - not only because of the expense of the panic/defer but because the function in general might, if it <em>does</em> exist, have side effects I don't want right now...</p>
  
  <p>Do I want something like a run-time implementation that mirrors the compiler's type check?  Or is there an easier way?   Am I thinking about this incorrectly?</p>
  
  <p><a href="https://play.golang.org/p/oumQBnJVD8" rel="nofollow noreferrer">Above example in a Go playground</a></p>