title: Golang Unmarshal slice of type interface
body: <p>In this example, I would be trying to load 2D scenes that contain polygons. In the code, I would have many different structs such as Circle, Square, Rectangle, Pentagon, etc.. 
  All would share common <em>funcs</em>, such as Area and Perimeter. 
  The scene itself would be stored as a slice of the interface Polygon.</p>
  
  <p>Here is the code I'm using to test this:</p>
  
  <pre><code>package main
  
  import (
      "encoding/json"
      "fmt"
      "math"
  )
  
  type Polygon interface {
      Area() float32
  }
  
  type Rectangle struct {
      Base   float32 `json:"base"`
      Height float32 `json:"height"`
      X      float32 `json:"x"`
      Y      float32 `json:"y"`
  }
  
  func (r *Rectangle) Area() float32 {
      return r.Base * r.Height
  }
  
  type Circle struct {
      Radius float32 `json:"radius"`
      X      float32 `json:"x"`
      Y      float32 `json:"y"`
  }
  
  func (c *Circle) Area() float32 {
      return c.Radius * c.Radius * math.Pi
  }
  
  func main() {
      rect := Rectangle{Base: 10, Height: 10, X: 10, Y: 10}
      circ := Circle{Radius: 10, X: 0, Y: 0}
  
      sliceOfPolygons := make([]Polygon, 0, 2)
  
      sliceOfPolygons = append(sliceOfPolygons, &amp;rect, &amp;circ)
  
      jsonData, err := json.Marshal(sliceOfPolygons)
      if err != nil {
          panic(err)
      }
  
      fmt.Println(string(jsonData))
  
      newSlice := make([]Polygon, 0)
  
      err = json.Unmarshal(jsonData, &amp;newSlice)
      if err != nil {
          panic(err)
      }
  }
  </code></pre>
  
  <p>In this example, I setup a slice of 2 polygons, marshal it and then try to unmarshal it again. 
  The marshal-ed string is:</p>
  
  <pre><code>[{"base":10,"height":10,"x":10,"y":10},{"radius":10,"x":0,"y":0}]
  </code></pre>
  
  <p>But when I try to <code>Unmarshal</code> it panics:</p>
  
  <pre><code>panic: json: cannot unmarshal object into Go value of type main.Polygon
  </code></pre>
  
  <p>If this worked it would be really useful and simple to use. I'd say that <code>Unmarshall</code> can't distinguish between a <code>Rectangle</code> and a <code>Circle</code> from the json string so it can't possibly know what struct to build. </p>
  
  <p>Is there any way to tag the struct or tell <code>Unmarshal</code> how to distinguish this structs?</p>