我正在尝试用 Go 编写一个播客下载器。以下代码解析 RSS 提要,但将解析后的数据打印到标准输出时,该频道的链接为空。我不知道为什么。有什么建议?我是 Go 的新手。
package main
import (
"encoding/xml"
"fmt"
"net/http"
)
type Enclosure struct {
Url string `xml:"url,attr"`
Length int64 `xml:"length,attr"`
Type string `xml:"type,attr"`
}
type Item struct {
Title string `xml:"title"`
Link string `xml:"link"`
Desc string `xml:"description"`
Guid string `xml:"guid"`
Enclosure Enclosure `xml:"enclosure"`
PubDate string `xml:"pubDate"`
}
type Channel struct {
Title string `xml:"title"`
Link string `xml:"link"`
Desc string `xml:"description"`
Items []Item `xml:"item"`
}
type Rss struct {
Channel Channel `xml:"channel"`
}
func main() {
resp, err := http.Get("http://www.bbc.co.uk/programmes/p02nrvz8/episodes/downloads.rss")
if err != nil {
fmt.Printf("Error GET: %v\n", err)
return
}
defer resp.Body.Close()
rss := Rss{}
decoder := xml.NewDecoder(resp.Body)
err = decoder.Decode(&rss)
if err != nil {
fmt.Printf("Error Decode: %v\n", err)
return
}
fmt.Printf("Channel title: %v\n", rss.Channel.Title)
fmt.Printf("Channel link: %v\n", rss.Channel.Link)
for i, item := range rss.Channel.Items {
fmt.Printf("%v. item title: %v\n", i, item.Title)
}
}
慕的地10843
相关分类