I'm trying a unmarshal some basic XML in Go. I've been able to unmarshal very large XML files in Go before, so the issue I have here really baffles me.
Unmarshalling finds one item, as it should, but all values are to their null default: strings are empty and the float is zero.
Any hint would help. Thanks.
<config><throttle delay="20" unit="s" host="feeds.feedburner.com"/></config>
host:"", unit:"", delay:0.000000
package main
import (
"encoding/xml"
"fmt"
)
// Config allows for unmarshling of the remote configuration file.
type Config struct {
XMLName xml.Name `xml:"config"`
Throttlers []*Throttler `xml:"throttle"`
}
// Throttler stores the throttle information read from the configuration file.
type Throttler struct {
host string `xml:"host,attr"`
unit string `xml:"unit,attr"`
delay float64 `xml:"delay,attr"`
}
func main() {
data := `
<config><throttle delay="20" unit="s" host="feeds.feedburner.com"/></config>
`
config := Config{}
err := xml.Unmarshal([]byte(data), &config)
if err != nil {
fmt.Printf("error: %config", err)
return
}
thr := config.Throttlers[0]
fmt.Println(fmt.Sprintf("host:%q, unit:%q, delay:%f", thr.host, thr.unit, thr.delay))
}
Just as trivially, the Throttler
struct doesn't export its fields. Hence, changing the struct to have upper case variables makes them accessible.