title: Golang embedding interfaces in struct with private access
body: <p>I want to replicate the following Java code in Golang in the most idiomatic way possible:</p>
  
  <pre class="lang-java prettyprint-override"><code>public class Handler {
  
      private Storage storage;
      private Mapper mapper;
  
      public Handler(Storage storage, Mapper mapper) {
          this.storage = storage;
          this.mapper = mapper;
      }
  
      public void handleKey(String k) {
          storage.put(k, mapper.map(k));
      }
  
  }
  
  interface Storage {
      public void put(String k, String v);
      public String get(String k);
  }
  
  @FunctionalInterface
  interface Mapper {
      public String map(String a);
  }
  
  private static class InMemoryStorage implements Storage {
      private Map&lt;String, String&gt; table;
  
      public InMemoryStorage() {
          table = new HashMap&lt;&gt;();
      }
  
      public void put(String k, String v) {
          table.put(k, v);
      }
  
      public String get(String k) {
          return table.get(k);
      }
  }
  </code></pre>
  
  <p><code>Handler</code> class only exposes the method <code>handleKey</code>. The behaviour of this class is parametrized by passing different concrete <code>Storage</code> and <code>Mapper</code> implementations.</p>
  
  <p>After reading <a href="https://golang.org/doc/effective_go.html#embedding" rel="nofollow noreferrer">Effective Go - Embedding</a>, I thought this would be a good use of embedding interfaces intro structs. But I can't figure out how to avoid exposing the embedded interfaces' methods to <code>Handler</code> users. I can do something like</p>
  
  <pre class="lang-golang prettyprint-override"><code>type Handler struct {
      store  Storage
      mapper Mapper
  }
  
  func (h Handler) Handle(k string) {
      h.store.Put(k, h.mapper.Map(k))
  }
  
  type Storage interface {
      Put(k string, v string)
      Get(k string) string
  }
  
  type Mapper interface {
      Map(k string) string
  }
  
  type inMemoryStorage {
      table map[string]string
  }
  
  func NewInMemoryStorage() Storage {
      return &amp;inMemoryStore{table: make(map[string]string)}
  }
  </code></pre>
  
  <p>but then I cannot pass concrete implementations to the <code>Handler</code> (creating struct literal) because <code>store</code> and <code>mapper</code> are unexported. And I do not want to create factory methods for each possible combination... Any suggestions?</p>