title: Dynamic function call in Go
body: <p>I'm trying to dynamically call functions returning different types of <code>struct</code>.</p>
  
  <p>For example, let's take the following code.</p>
  
  <pre><code>struct A {
     Name string
     Value  int
  }
  
  struct B {
     Name1 string
     Name2 string
     Value   float
  }
  
  func doA() (A) {
     // some code returning A
  }
  
  func doB() (B) {
     // some code returning B
  }
  </code></pre>
  
  <p>I would like to pass either the function <code>doA</code> or <code>doB</code> as an argument to a generic function that would execute the function and JSON-encode the result. Like the following:</p>
  
  <pre><code>func Generic(w io.Writer, fn func() (interface {}) {
      result := fn()
      json.NewEncoder(w).Encode(result)
  }
  </code></pre>
  
  <p>But when I do:</p>
  
  <pre><code>Generic(w, doA)
  </code></pre>
  
  <p>I get the following error:</p>
  
  <pre><code>cannot use doA (type func() (A)) as type func() (interface {})
  </code></pre>
  
  <p>Is there a way to achieve this dynamic call?</p>