title: golang conversion between function accepting abstract interface and function accepting struct implementation
body: <p>An issue I met when creating a layer on top of different version of implementations. The goal is to abstract out the implementation details and the caller doesn't need to care which implementation we are using.</p>
  
  <p>Please see the code example <a href="https://play.golang.org/p/SyuirDQkH9Z" rel="nofollow noreferrer">here</a></p>
  
  <p>^ The code can better explain the issue I met.</p>
  
  <p>We have two version of Stream implementation <code>Stream1</code> and <code>Stream2</code>. They have a common interface <code>Stream</code>.</p>
  
  <p>Both of them have a corresponding <code>BindStreamHandler</code> function accepting <code>StreamHandler1</code> or <code>StreamHandler2</code>.</p>
  
  <p>We have a function <code>BindStreamHandler</code>, and a general implementation of <code>func StreamHandlerImpl(s Stream)</code>. No matter we use <code>Stream1</code> or <code>Stream2</code>, the general implementation is the same.</p>
  
  <p>Now I face an issue downcasting <code>StreamHandlerImpl</code> (accepting abstract <code>Stream</code> to <code>StreamHandler1</code> (accepting <code>Stream1</code>).</p>
  
  <hr>
  
  <p>Update:</p>
  
  <p>I found <a href="https://play.golang.org/p/M21MZr3L_Sj" rel="nofollow noreferrer">this version</a> worked.</p>
  
  <pre><code>func BindHandler(h interface{}) {
      if Version == 1 {
          h1 := h.(StreamHandler1)
          BindStreamHandler1(h1)
      } else {
          h2 := h.(StreamHandler2)
          BindStreamHandler2(h2)
      }
  }
  </code></pre>
  
  <p>But the signature of <code>BindHandler</code> becomes too weak :( I prefer using signature <code>func BindHandler(h StreamHandler)</code></p>