title: Dynamically call method on interface{} regardless of receiver type
body: <p>I'm working on a templating system written in Go, which means it requires liberal use of the <code>reflect</code> package. In this specific circumstance I need to be able to dynamically call a method on an <code>interface{}</code>. The oddity is that my reflection logic works fine as long as my data is of a known type, but not if the data is of type <code>interface{}</code>.</p>
  
  <p>The the following example you can see that the logic in <code>main()</code> and <code>Pass()</code> is identical. The only difference is whether the data is a known type or a known type inside an <code>interface{}</code></p>
  
  <p>Go Play: <a href="http://play.golang.org/p/FTP3wgc0sZ" rel="noreferrer">http://play.golang.org/p/FTP3wgc0sZ</a></p>
  
  <pre><code>package main
  
  import (
      "fmt"
      "reflect"
  )
  
  type Test struct {
      Start string
  }
  
  func (t *Test) Finish() string {
      return t.Start + "finish"
  }
  
  func Pass(i interface{}) {
      _, ok := reflect.TypeOf(&amp;i).MethodByName("Finish")
      if ok {
          fmt.Println(reflect.ValueOf(&amp;i).MethodByName("Finish").Call([]reflect.Value{})[0])
      } else {
          fmt.Println("Pass() fail")
      }
  }
  
  func main() {
      i := Test{Start: "start"}
  
      Pass(i)
      _, ok := reflect.TypeOf(&amp;i).MethodByName("Finish")
      if ok {
          fmt.Println(reflect.ValueOf(&amp;i).MethodByName("Finish").Call([]reflect.Value{})[0])
      } else {
          fmt.Println("main() fail")
      }
  }
  </code></pre>
  
  <p>Upon executing this code we get the following result</p>
  
  <pre><code>Pass() fail
  startfinish
  </code></pre>
  
  <p>Which means that my methodology for dynamically calling a method works fine except in a scenario when my object is currently in an <code>interface{}</code>.</p>
  
  <p>If instead I do not use a pointer receiver and pass <code>i</code> then it works as expected.</p>
  
  <p>Go Play: <a href="http://play.golang.org/p/myM0UXVYzX" rel="noreferrer">http://play.golang.org/p/myM0UXVYzX</a></p>
  
  <p>This leads me to believe that my problem is that I cannot access the address of i (<code>&amp;i</code>) when it is an <code>interface{}</code>. I've scoured the reflect package and tested things such as <code>reflect.Value.Addr()</code> and <code>reflect.PtrTo()</code> but I could not get either to work the way I needed. My hunch is that it has something to do with the fact that an <code>interface{}</code> is by definition a reference object.</p>