title: Nested interfaces in Go
body: <p>I have an interface type in Go, which defines some methods, with their own nested interfaces. Something like this:</p>
  
  <pre><code>type Store interface {
      Pack(key string) interface {
          Put(r io.Reader) (int, error)
          io.Closer
      }
      // ... other stuff ...
  }
  </code></pre>
  
  <p>Now, I define another interface, let's call it a Packer, which matches what the Pack method returns, like this:</p>
  
  <pre><code>type Packer interface {
      Put(r io.Reader) (int, error)
      io.Closer
  }
  </code></pre>
  
  <p>I can then implement two kinds of Stores. One with a Pack method that returns a Packer, and another with a Pack that returns the explicit interface type:</p>
  
  <pre><code>func (s *storeTypeA) Pack(key string) Packer { ... }
  
  func (s *storeTypeB) Pack(key string) interface {
      Put(r io.Reader) (int, error)
      io.Closer
  } { ... }
  </code></pre>
  
  <p>There's only one problem now. <code>storeTypeA</code> doesn't actually conform to the Store interface anymore. If I try to pass it off as a Store, I get this error:</p>
  
  <pre><code>prog.go: cannot use storeTypeA literal (type *storeTypeA) as type Store in assignment:
      *storeTypeA does not implement Store (wrong type for Pack method)
          have Pack(string) Packer
          want Pack(string) interface { Close() error; Put(io.Reader) (int, error) }
  </code></pre>
  
  <p>What I don't understand though, is that the Packer interface <em>is</em> exactly equal to <code>interface { Close() error; Put(io.Reader) (int, error) }</code>. So why the error?</p>
  
  <p>Any ideas how to work around this?</p>
  
  <p>I guess the obvious thing would be to just not have the nested interfaces, but I'm kind of curious why this should be a problem. I'm doing it this way because I want to be able to define the interface <em>outside</em> of the package where I'm providing the Store itself.</p>