title: Golang xml.Unmarshal interface types
body: <p>Using the <code>xml</code> package in golang I'm having trouble unmarshalling a list of non-homogenous types.  Consider the following XML document whose nested elements are a list of non-homogenous types:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;mydoc&gt;
    &lt;foo&gt;Foo&lt;/foo&gt;
    &lt;bar&gt;Bar&lt;/bar&gt;
    &lt;foo&gt;Another Foo&lt;/foo&gt;
    &lt;foo&gt;Foo #3&lt;/foo&gt;
    &lt;bar&gt;Bar 2&lt;/bar&gt;
  &lt;/mydoc&gt;
  </code></pre>
  
  <p>And the following golang code to test XML un/marshalling (also here on <a href="http://play.golang.org/p/aG3IglaI9w" rel="nofollow">the go playground</a>):</p>
  
  <pre class="lang-go prettyprint-override"><code>package main
  
  import "encoding/xml"
  import "fmt"
  
  const sampleXml = `
  &lt;mydoc&gt;
    &lt;foo&gt;Foo&lt;/foo&gt;
    &lt;bar&gt;Bar&lt;/bar&gt;
    &lt;foo&gt;Another Foo&lt;/foo&gt;
    &lt;foo&gt;Foo #3&lt;/foo&gt;
    &lt;bar&gt;Bar 2&lt;/bar&gt;
  &lt;/mydoc&gt;
  `
  
  type MyDoc struct {
    XMLName xml.Name `xml:"mydoc"`
    Items   []Item
  }
  
  type Item interface {
    IsItem()
  }
  
  type Foo struct {
    XMLName xml.Name `xml:"foo"`
    Name    string   `xml:",chardata"`
  }
  
  func (f Foo) IsItem() {}
  
  type Bar struct {
    XMLName xml.Name `xml:"bar"`
    Nombre  string   `xml:",chardata"`
  }
  
  func (b Bar) IsItem() {}
  
  func main() {
    doMarshal()
    doUnmarshal()
  }
  
  func doMarshal() {
    myDoc := MyDoc{
      Items: []Item{
        Foo{Name: "Foo"},
        Bar{Nombre: "Bar"},
        Foo{Name: "Another Foo"},
        Foo{Name: "Foo #3"},
        Bar{Nombre: "Bar 2"},
      },
    }
    bytes, err := xml.MarshalIndent(myDoc, "", "  ")
    if err != nil {
      panic(err)
    }
    // Prints an XML document just like "sampleXml" above.
    println(string(bytes))
  }
  
  func doUnmarshal() {
    myDoc := MyDoc{}
    err := xml.Unmarshal([]byte(sampleXml), &amp;myDoc)
    if err != nil {
      panic(err)
    }
    // Fails to unmarshal the "Item" elements into their respective structs.
    fmt.Printf("ERR: %#v", myDoc)
  }
  </code></pre>
  
  <p>You'll see that <code>doMarshal()</code> produces the exact XML document I expect; however, <code>doUnmarshal()</code> fails to deserialize the "Item" elements into their respective structs.  I've tried a few changes but nothing seems to get them to unmarshal properly (creating storage for <code>myDoc.Items</code>, changing the type of "Items" to <code>[]*Item</code> [and others], fiddling with the XML tags, etc).</p>
  
  <p>Any ideas how to get <code>xml.Unmarshal(...)</code> to deserialize a list of elements of unrelated types?</p>