douqianni4080 2013-09-21 18:12
浏览 54
已采纳

为什么使用go-pkg-rss不能返回Atom项目标题?

Either I am losing my mind, I don't understand arrays of pointers, or the library is busted. Perhaps you can help. The following code seems to fetch Atom items just fine, but viewing their titles are coming back as empty strings.

(This happens to be implemented via http, I don't think that's the problem but of course anything's possible.)

package main

import (
    "fmt"
    rss "github.com/jteeuwen/go-pkg-rss"
    "net/http"
    "os"
)

var items []*rss.Item
var channels []*rss.Channel

func hello(w http.ResponseWriter, r *http.Request) {
    feed := rss.New(5, true, chanHandler, itemHandler)
    url := "http://stackoverflow.com/feeds"
    feed.Fetch(url, nil)
    fmt.Printf("Sent fetch for %s
", url)
    fmt.Fprintf(w, "There are %d items in %s

", len(items), url)
    for key, value := range items {
        fmt.Fprintf(w, "%d: %s

", key, value.Title)
    }
}

func main() {
    http.HandleFunc("/", hello)
    http.ListenAndServe(":"+os.Getenv("PORT"), nil)
}

func chanHandler(feed *rss.Feed, newchannels []*rss.Channel) {
    channels = newchannels
}

func itemHandler(feed *rss.Feed, ch *rss.Channel, newitems []*rss.Item) {
    items = newitems
}
  • 写回答

3条回答 默认 最新

  • dongqu5650 2013-09-21 20:59
    关注

    There is some bug with package "go-pkg-rss" about reading response.

    There is a lot of similar packages, i tried to replicate your example with another one (it's simpler i think):

    import (
        "fmt"
        "github.com/SlyMarbo/rss"
        "net/http"
        "os"
    )
    
    func hello(w http.ResponseWriter, r *http.Request) {
        url := "http://stackoverflow.com/feeds"
        feed, _ := rss.Fetch(url)
        fmt.Printf("Sent fetch for %s
    ", url)
        fmt.Fprintf(w, "There are %d items in %s
    
    ", len(feed.Items), url)
        for key, value := range feed.Items {
            fmt.Fprintln(w, key, value.Title)
        }
    }
    
    func main() {
        http.HandleFunc("/", hello)
        http.ListenAndServe(":"+os.Getenv("PORT"), nil)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 MATLAB APP 制作出现问题
  • ¥15 wannier复现图像时berry曲率极值点与高对称点严重偏移
  • ¥15 利用决策森林为什么会出现这样·的问题(关键词-情感分析)
  • ¥15 DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI[/untitled30_war_e
  • ¥15 使用deepspeed训练,发现想要训练的参数没有梯度
  • ¥15 寻找一块做为智能割草机的驱动板(标签-stm32|关键词-m3)
  • ¥15 信息管理系统的查找和排序
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),怎么用读取的电磁传感器信号表示小车所在的位置
  • ¥15 如何解决y_true和y_predict数据类型不匹配的问题(相关搜索:机器学习)
  • ¥15 PB中矩阵文本型数据的总计问题。