I've been playing about with Go's XML package and cannot see what is wrong with the following code.
package main
import (
"encoding/xml"
"fmt"
"net/http"
)
type Channel struct {
Items Item
}
type Item struct {
Title string `xml:"title"`
Link string `xml:"link"`
Description string `xml:"description"`
}
func main() {
var items = new(Channel)
res, err := http.Get("http://www.reddit.com/r/google.xml")
if err != nil {
fmt.Printf("Error: %v\n", err)
} else {
decoded := xml.NewDecoder(res.Body)
err = decoded.Decode(items)
if err != nil {
fmt.Printf("Error: %v\n", err)
}
fmt.Printf("Title: %s\n", items.Items.Title)
}
}
The above code runs without any errors and prints to the terminal:
Title:
The struct seems empty but I can't see why it isn't getting populated with the XML data.
I'd be completely explicit like this - name all the XML parts
See the playground for a full working example
type Rss struct {
Channel Channel `xml:"channel"`
}
type Channel struct {
Title string `xml:"title"`
Link string `xml:"link"`
Description string `xml:"description"`
Items []Item `xml:"item"`
}
type Item struct {
Title string `xml:"title"`
Link string `xml:"link"`
Description string `xml:"description"`
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With