title: Go Interface Fields
body: <p>I'm familiar with the fact that, in Go, interfaces define functionality, rather than data.  You put a set of methods into an interface, but you are unable to specify any fields that would be required on anything that implements that interface.</p>
  
  <p>For example: </p>
  
  <pre><code>// Interface
  type Giver interface {
      Give() int64
  }
  
  // One implementation
  type FiveGiver struct {}
  
  func (fg *FiveGiver) Give() int64 {
      return 5
  }
  
  // Another implementation
  type VarGiver struct {
      number int64
  }
  
  func (vg *VarGiver) Give() int64 {
      return vg.number
  }
  </code></pre>
  
  <p>Now we can use the interface and its implementations:</p>
  
  <pre><code>// A function that uses the interface
  func GetSomething(aGiver Giver) {
      fmt.Println("The Giver gives: ", aGiver.Give())
  }
  
  // Bring it all together
  func main() {
      fg := &amp;FiveGiver{}
      vg := &amp;VarGiver{3}
      GetSomething(fg)
      GetSomething(vg)
  }
  
  /*
  Resulting output:
  5
  3
  */
  </code></pre>
  
  <p>Now, what you <em>can't</em> do is something like this:</p>
  
  <pre><code>type Person interface {
      Name string
      Age int64
  }
  
  type Bob struct implements Person { // Not Go syntax!
      ...
  }
  
  func PrintName(aPerson Person) {
      fmt.Println("Person's name is: ", aPerson.Name)
  }
  
  func main() {
      b := &amp;Bob{"Bob", 23}
      PrintName(b)
  }
  </code></pre>
  
  <p>However, after playing around with interfaces and embedded structs, I've discovered a way to do this, after a fashion:</p>
  
  <pre><code>type PersonProvider interface {
      GetPerson() *Person
  }
  
  type Person struct {
      Name string
      Age  int64
  }
  
  func (p *Person) GetPerson() *Person {
      return p
  }
  
  type Bob struct {
      FavoriteNumber int64
      Person
  }
  </code></pre>
  
  <p>Because of the embedded struct, Bob has everything Person has.  It also implements the PersonProvider interface, so we can pass Bob into functions that are designed to use that interface.</p>
  
  <pre><code>func DoBirthday(pp PersonProvider) {
      pers := pp.GetPerson()
      pers.Age += 1
  }
  
  func SayHi(pp PersonProvider) {
      fmt.Printf("Hello, %v!\r", pp.GetPerson().Name)
  }
  
  func main() {
      b := &amp;Bob{
          5,
          Person{"Bob", 23},
      }
      DoBirthday(b)
      SayHi(b)
      fmt.Printf("You're %v years old now!", b.Age)
  }
  </code></pre>
  
  <p><a href="http://play.golang.org/p/s9A8M7eT3i">Here is a Go Playground</a> that demonstrates the above code.</p>
  
  <p>Using this method, I can make an interface that defines data rather than behavior, and which can be implemented by any struct just by embedding that data.  You can define functions that explicitly interact with that embedded data and are unaware of the nature of the outer struct.  And everything is checked at compile time!  (The only way you could mess up, that I can see, would be embedding the interface <code>PersonProvider</code> in <code>Bob</code>, rather than a concrete <code>Person</code>.  It would compile and fail at runtime.)</p>
  
  <p>Now, here's my question: is this a neat trick, or should I be doing it differently?</p>