Created
July 14, 2011 16:19
-
-
Save kylelemons/1082781 to your computer and use it in GitHub Desktop.
A potential bug in unmarshal with named slices
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
err <nil> | |
val &main.Slice{ID:"himynameis", Number:[]string{"1", "2", "3"}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
err <nil> | |
val &main.Slice{ID:"", Number:[]string{}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
* 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