title: How to create an interface method with multiple return types
body: <p>This is a follow up to this question: <a href="https://stackoverflow.com/questions/45055953/interface-method-with-multiple-return-types/45057403?noredirect=1#comment107273413_45057403">Interface method with multiple return types</a></p>
  
  <p>I have two structs which are slightly different. One is about a trade struct, the other about a transfer struct. The goal is to calculate the quantity at the end. Also the trade struct shall implement some specific function not common to the transfer struct and the other way around. At the end they all call a <code>get()</code> function and ultimately return the quantity (type string). I can't come to do something like that <code>qtyGetService(trade{}).calculateA().get()</code> where <code>qtyGetService()</code> and <code>get()</code> can be called from both struct, but <code>calculateA()</code> is a method only for trade struct. Interfaces look at first promising to solve such issues but I'm facing the issue described in that question <a href="https://stackoverflow.com/questions/45055953/interface-method-with-multiple-return-types/45057403">Interface method with multiple return types</a> that a method in an interface must return a specific type. Returning an <code>interface{}</code> wouldn't be an option either as I would not be able to chain the functions like shown in the example below (not even mentioning the use of <code>reflect</code>).</p>
  
  <pre class="lang-golang prettyprint-override"><code>package main
  
  import (
      "fmt"
  )
  
  type Trade struct {
      q   string
      // many other attributes
  }
  
  type Transfer struct {
      q   string
      // many other attributes
  }
  
  type TradeService struct {
      q       string
      // some other attributes
  }
  
  type TransferService struct {
      q       string
      // some other attributes
  }
  
  type GetQty struct {
      q       string
      // some other attributes
  }
  
  func qtyGetTradeService(str *Trade) *TradeService {
      // some processing on the initial struct
      return &amp;TradeService{
          q: str.q + " TradeService ",
      }
  }
  
  func qtyGetTransferService(str *Transfer) *TransferService {
      // some processing on the initial struct
      return &amp;TransferService{
          q: str.q + " TransferService ",
      }
  }
  
  func (ts *TradeService) calculateA() *GetQty {
      // some processing on ts
      return &amp;GetQty{
          q: ts.q + " CalculateA ",
      }
  }
  
  func (ts *TradeService) calculateB() *GetQty {
      // some processing on ts
      return &amp;GetQty{
          q: ts.q + " CalculateB ",
      }
  }
  
  func (ts *TransferService) calculateC() *GetQty {
      // some processing on ts
      return &amp;GetQty{
          q: ts.q + " CalculateC ",
      }
  }
  
  func (gq *GetQty) get() string{
      // some processing on gq common to both trade and transfer
      return gq.q + " CommonGet "
  }
  
  
  
  func main() {
      // this works just fine
      fmt.Println(qtyGetTradeService(&amp;Trade{q: "10"}).calculateA().get())
      fmt.Println(qtyGetTransferService(&amp;Transfer{q: "10"}).calculateC().get())
  
      // But this would be "better" to do something like this:
      fmt.Println(qtyGetService(&amp;Trade{q: "10"}).calculateA().get())
      fmt.Println(qtyGetService(&amp;Transfer{q: "10"}).calculateC().get())
  }
  </code></pre>
  
  <p>Link to playground: <a href="https://play.golang.org/p/SBCs_O9SL0k" rel="nofollow noreferrer">https://play.golang.org/p/SBCs_O9SL0k</a></p>