Skip to content

Instantly share code, notes, and snippets.

@EtienneR
Created August 16, 2015 22:29
Show Gist options
  • Save EtienneR/b2eb640ca959386164ff to your computer and use it in GitHub Desktop.
Save EtienneR/b2eb640ca959386164ff to your computer and use it in GitHub Desktop.
RSS part5 ("pubDate")
package main
import (
"encoding/xml"
"net/http"
"time"
)
func main() {
http.HandleFunc("/", rssHandler)
http.ListenAndServe(":3000", nil)
}
func rssHandler(w http.ResponseWriter, r *http.Request) {
type Item struct {
Title string `xml:"title"`
Link string `xml:"link"`
Description string `xml:"description"`
PubDate string `xml:"pubDate"`
}
type rss struct {
Version string `xml:"version,attr"`
Description string `xml:"channel>description"`
Link string `xml:"channel>link"`
Title string `xml:"channel>title"`
Item []Item `xml:"channel>item"`
}
articles := []Item{
{"foo", "http://mywebsite.com/foo", "lorem ipsum", time.Now().Format(time.RFC1123Z)},
{"foo2", "http://mywebsite.com/foo2", "lorem ipsum2", time.Now().Format(time.RFC1123Z)}}
feed := &rss{
Version: "2.0",
Description: "My super website",
Link: "http://mywebsite.com",
Title: "Mywebsite",
Item: articles,
}
x, err := xml.MarshalIndent(feed, "", " ")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/xml")
w.Write(x)
}
<rss version="2.0">
<channel>github.com/russross/blackfriday
<description>My super website</description>
<link>http://mywebsite.com</link>
<title>Mywebsite</title>
<item>
<title>foo</title>
<link>http://mywebsite.com/foo</link>
<description>lorem ipsum</description>
<pubDate>Sun, 09 Aug 2015 16:05:14 +0200</pubDate>
</item>
<item>
<title>foo2</title>
<link>http://mywebsite.com/foo2</link>
<description>lorem ipsum2</description>
<pubDate>Sun, 09 Aug 2015 16:05:14 +0200</pubDate>
</item>
</channel>
</rss>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment