March 17th, 2016
起因
想要試著寫RSS render,其實並沒有那麼困難.
步驟
先建立一個 tmpl file (ex: atom.tmpl)
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title></title>
<link></link>
<atom:link href="" rel="self" type="application/rss+xml" />
<description></description>
<pubDate></pubDate>
<lastBuildDate></lastBuildDate>
<item>
<title></title>
<description></description>
<pubDate></pubDate>
<guid></guid>
<link></link>
</item>
</channel>
</rss>
然後再Code裡面,去使用html/template
func AtomRender() string {
var r SomeStuct
//讀入template
t, err := template.ParseFiles("atom.tmpl")
if err != nil {
log.Fatal(err)
}
// log.Println("parse template")
//產生io.writer buffer
buf := new(bytes.Buffer)
//讀取資料
err = t.Execute(buf, r)
// log.Println("execute it")
if err != nil {
log.Fatal(err)
}
// log.Println("get rss:", buf.String())
return buf.String()
}
雷
”+”, “-“ charactor handle
想要在html 裡面parse +
或是-
,可別直接當成string
.
//定義成 template.HTML
RssNow template.HTML
//
t.RssNow = template.HTML(SomeString)
跟RFC822對於W3C Feed Validator的問題
問題:
Hi All, I trying to write a feed render and I found the time.Format(time.RFC822) is not valid by W3C feed validator . Here is the code http://play.golang.org/p/mfzLKsuVOW
Not sure if it is a issue, or I misunderstanding.
得到解答:
- try
time.RFC1123
.seems to be close enough
–> RFC1123 seems much close but w3c feed validator will fail.
- RFC822 only specifies 2 digit years, so I think go is doing the correct thing. it would appear the w3c validator is interpreting rfc822 dates according to some other interpretation of RFC822
官方有類似的問題: Golang issue