Last active
February 26, 2020 11:01
-
-
Save baptistedonaux/c52a56540de5caeef743 to your computer and use it in GitHub Desktop.
Extract RSS in Golang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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