title: How to implement or mock a Go interface with reflection?
body: <p>I want to implement Go interfaces with reflection to generate mocks and stubs. But if I look at the <a href="http://golang.org/pkg/reflect/" rel="nofollow">reflect</a> package, I get no idea how to do it (maybe it is not possible). </p>
  
  <p>Example: Testing that a func calls <code>WriteHeader(404)</code> on <a href="http://golang.org/pkg/net/http/#ResponseWriter" rel="nofollow">ResponseWriter</a>:</p>
  
  <pre><code>type ResponseWriterMock struct {                              //
    status int                                                  //
  }                                                             // How to replace
  func (*ResponseWriterMock) Header() Header {}                 // this block with 
  func (*ResponseWriterMock) Write([]byte) (i int, e error) {}  // a reflectivly 
  func (m *ResponseWriterMock) WriteHeader(status int) {        // generated mock? 
    m.status = status                                           //  
  }                                                             //
  responseWriterMock := new(ResponseWriterMock)
  
  funcToTest(responseWriterMock)
  
  if responseWriterMock.status != 404 {
      // report error
  }
  </code></pre>
  
  <p>With RhinoMocks (C#) I would write it like this:</p>
  
  <pre><code>var responseWriterMock = MockRepository.GenerateMock&lt;ResponseWriter&gt;();
  
  funcToTest(responseWriterMock);
  
  responseWriterMock.AssertWasCalled(rw =&gt; rw.WriteHeader(404));
  </code></pre>
  
  <p>How can I implement Go interfaces with reflection?</p>
  
  <p><strong>Addendum</strong></p>
  
  <p><a href="http://code.google.com/p/go/issues/detail?id=4146" rel="nofollow">Seems like it is not possible today.</a></p>