title: Golang interfaces & mocking
body: <p>I'm having hard times writing unit tests in Go due to external libraries which don't expose an interface (therefore not mockable) but only pure functions. Even big ones like <a href="https://godoc.org/cloud.google.com/go/redis/apiv1#CloudRedisClient" rel="noreferrer">Google don't</a>, so I'm wondering whether my approach is good enough.
  Wouldn't be good practice for libraries to provide <code>interface</code>s instead of packages with only functions in order to let the user mock them? </p>
  
  <p>The solution I came up with until now is wrap these packages with an interface's implementation but that seem like too much work. </p>
  
  <p>I come with an example. My function could look like this</p>
  
  <pre><code>func AnyFunction() error {
      sess := session.Get("blabla")
      // logic in here...
  }
  </code></pre>
  
  <p>where session is an imported package that returns a <code>struct</code>. I can't mock the package <code>session</code>.
  For this case I'm going to write a <code>SessionInterface</code> with an implementation, which internally calls session.</p>
  
  <p>Ex:</p>
  
  <pre><code>type SessionInterface interface {
      Get(s string) Session
  }
  
  type mySessionImpl struct {}
  func (me *mySessionImpl) Get(s string) Session {
    return session.Get(s)
  }
  </code></pre>
  
  <p>For my tests now I can mock the SessionInterface and inject that one into my code</p>