casualjim
7/2/2016 - 12:00 AM

run this: https://play.golang.org/p/BWDlpVkoru

package main

import (
	"fmt"
)

type Gettable interface {
  Name() string
}

type gettable struct {
  name string
}
func (g *gettable) Name() string {
  return g.name
}

func (g *gettable) SetName(name string) {
  g.name = name
}

func main(){
  g := &gettable{"some name"}
  fmt.Println("original:", g.Name())
  
  // interfaces don't really have to exist anywhere except on a var
  var settable interface{
    SetName(string)
  }
  settable = g
  settable.SetName("a new name")
  fmt.Println("new:", g.Name())

  // you can define types inside method bodies
  type setter interface {
    SetName(string)
  }
  var s setter = g
  s.SetName("yet another name")
  fmt.Println("new 2:", g.Name())
}