Skip to content

Instantly share code, notes, and snippets.

@kylelemons
Created July 14, 2011 16:19
Show Gist options
  • Save kylelemons/1082781 to your computer and use it in GitHub Desktop.
Save kylelemons/1082781 to your computer and use it in GitHub Desktop.
A potential bug in unmarshal with named slices
package main
import (
"fmt"
"xml"
"bytes"
)
type Slice struct {
ID string "ident" // or `xml:"ident"` with a newish weekly
Number []string "num" // or `xml:"num"`
}
func main() {
s := &Slice{}
fmt.Println("err", xml.Unmarshal(bytes.NewBufferString(`
<slice>
<ident>himynameis</ident>
<num>1</num>
<num>2</num>
<num>3</num>
</slice>
`), s))
fmt.Printf("val %#v\n", s)
}
err <nil>
val &main.Slice{ID:"himynameis", Number:[]string{"1", "2", "3"}}
err <nil>
val &main.Slice{ID:"", Number:[]string{}}
* If the XML element contains a sub-element whose name
matches a field whose tag is neither "attr" nor "chardata",
Unmarshal maps the sub-element to that struct field.
Otherwise, if the struct has a field named Any, unmarshal
maps the sub-element to that struct field.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment