Skip to content

Instantly share code, notes, and snippets.

@baptistedonaux
Last active February 26, 2020 11:01
Show Gist options
  • Save baptistedonaux/c52a56540de5caeef743 to your computer and use it in GitHub Desktop.
Save baptistedonaux/c52a56540de5caeef743 to your computer and use it in GitHub Desktop.
Extract RSS in Golang
package main
import (
"encoding/xml"
"io/ioutil"
"log"
"os"
)
type Item struct {
XMLName xml.Name `xml:"item"`
Description string `xml:"description"`
Link string `xml:"link"`
PubDate string `xml:"pubDate"`
Title string `xml:"title"`
}
type Rss struct {
XMLName xml.Name `xml:"rss"`
Items []Item `xml:"channel>item"`
}
func main() {
file, err := ioutil.ReadFile("/go/news.rss")
if err != nil {
log.Println(err)
return
}
v := Rss{}
err = xml.Unmarshal(file, &v)
if err != nil {
log.Println(err.Error())
os.Exit(1)
}
for _, item := range v.Items {
log.Println("Title: ", item.Title)
log.Println("Description: ", item.Description)
log.Println("Link: ", item.Link)
log.Println("PubDate: ", item.PubDate)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment