jLuger.de - RSS parsing with go

How to parse RSS in Go? Define the following types:

type Item struct {
    Title string `xml:"title"`
    Link string `xml:"link"`
    Description string `xml:"description"`
    Guid string `xml:"guid"`
    PubDate string `xml:"pubDate"`
}

type Channel struct {
    Title string `xml:"title"`
    Link string `xml:"link"`
    Items []Item `xml:"item"`
}

type Rss struct {
    Version string `xml:"version,attr"`
    Channel Channel `xml:"channel"`
}

And then use this code:

    var rss Rss
    decoder := xml.NewDecoder(r)
    err := decoder.Decode(&rss)

When the RSS isn't encoded in UTF-8 you have to add a CharsetReader.

And that's it. Oh wait. Have you seen this line:

    PubDate string `xml:"pubDate"`

A date that is stored as a string? Yes, the default parser of go's date type isn't compatible with the date format used by RSS. I haven't found to use set another date format and even then it wouldn't be enough as the RSS date format has some flexibility that can't be done with one simple parse layout required by Go.

To get around I've created a parsing function and two data structure trees. One for parsing the xml and one for working.

You are laying on the ground and yelling DRY? Calm down, Go has some medicine to reduce your pain. See:

type baseItem struct {
    Title string `xml:"title"`
    Link string `xml:"link"`
    Description string `xml:"description"`
    Guid string `xml:"guid"`
}

type xmlItem struct {
    baseItem
    PubDate string `xml:"pubDate"`
}

type Item struct {
    baseItem
    PubDate *time.Time
}

To get the common data from xmlItem to Item you can do this:

    var i Item;
    i.baseItem = xmlI.baseItem

On line to copy four elements. Quite cool