我正在使用 Go 模板输出 html,并通过管道插入一些值。事情是我不想被转义的原始 html 的值之一。但是当模板被执行时,它被转义了。
这是代码
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"html/template"
"encoding/xml"
)
type RSS struct {
XMLName xml.Name `xml:"rss"`
Items Items `xml:"channel"`
}
type Items struct {
XMLName xml.Name `xml:"channel"`
ItemList []Item `xml:"item"`
}
type Item struct {
Title string `xml:"title"`
Link string `xml:"link"`
Description string `xml:"description"`
}
func main() {
res, err := http.Get("http://news.google.com/news?hl=en&gl=us&q=samsung&um=1&ie=UTF-8&output=rss")
if err != nil {
log.Fatal(err)
}
asText, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}
var i RSS
err = xml.Unmarshal([]byte(asText), &i)
if err != nil {
log.Fatal(err)
}
res.Body.Close()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
handler(w, r, i)
})
http.ListenAndServe(":8080", nil)
}
func handler(w http.ResponseWriter, r *http.Request, i RSS) {
t, _ := template.ParseFiles("index.html")
t.Execute(w, i.Items)
}
这是html:
<html>
<head>
</head>
<body>
{{range .ItemList}}
<div class="news-item">
<p>
<a href="{{.Link}}">{{.Title}}</a>
</p>
<p>{{.Description}}</p>
</div>
{{end}}
</body>
</html>
输出如下所示:
<div class="news-item">
<p>
<a href="http://news.google.com/news/url?sa=t&fd=R&usg=AFQjCNFd-5CF7Rwy7sjNZ2-fSOLkO6ri5g&url=http://www.pehub.com/186539/what-apple-might-learn-samsung/">What Apple Might Learn from Samsung - Private Equity Hub (press release)</a>
</p>
<p><table border="0" cellpadding="2" cellspacing="7" style="vertical-align:top;"><tr><td width="80" align="center" valign="top"><font style="font-size:85%;font-family:arial,sans-serif"></font></td><td valign="top" class="j"><font style="font-size:85%;font-family:arial,sans-serif"><br /><div style="padding-top:0.8em;"><img alt="" height="1" width="1" /></div><div class="lh"><a href="http://news.google.com/news/url?sa=t&amp;fd=R&amp;usg=AFQjCNFd-5CF7Rwy7sjNZ2-fSOLkO6ri5g&amp;url=http://www.pehub.com/186539/what-apple-might-learn-samsung/"><b>What Apple Might Learn from <b>Samsung</b></b></a><br /><font size="-1"><b><font color="#6f6f6f">Private Equity Hub (press release)</font></b></font><br /><font size="-1"><b>Samsung</b> suddenly seems a lot like a boxer whose every punch at the world champion, Apple, is bringing it closer to a legitimate shot at the title. <b>Samsung&#39;s</b> handsets are hot. Late last year, <b>Samsung&#39;s</b> Galaxy S III became the best-selling smartphone in <b>...</b></font><br /><font size="-1" class="p"></font><br /><font class="p" size="-1"><a class="p" href="http://news.google.com/news/more?ncl=d2dovyDH3OFX_MM&amp;ned=us"><nobr><b></b></nobr></a></font></div></font></td></tr></table></p>
</div>
描述是转义的 html,我想要它是常规的 html