Skip to content

Instantly share code, notes, and snippets.

Created June 20, 2013 18:28
Show Gist options
  • Save anonymous/5825288 to your computer and use it in GitHub Desktop.
Save anonymous/5825288 to your computer and use it in GitHub Desktop.
Unable to unmarshal int with spaces
package main
import (
"encoding/xml"
"fmt"
)
type MyType struct {
XMLName xml.Name `bson:"-" xml:"response"`
Result int `bson:"result" xml:"result"`
}
func main() {
var payload string
payload = `
<response>
<result>1</result>
</response>
`
var mt MyType
xml.Unmarshal([]byte(payload), &mt)
// This prints: {XMLName:{Space: Local:response} Result:1}
fmt.Printf("mt was %+v\n", mt)
// NOTE: result has spaces
payload = `
<response>
<result> 1 </result>
</response>
`
var newmt MyType
xml.Unmarshal([]byte(payload), &newmt)
// This prints: {XMLName:{Space: Local:response} Result:0}
fmt.Printf("newmt was %+v\n", newmt)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment