为什么使用go-pkg-rss时Atom项目标题没有返回?

huangapple go评论84阅读模式
英文:

Why are Atom item titles not being returned using go-pkg-rss?

问题

要翻译的内容如下:

要么是我疯了,要么是我不理解指针数组,要么是库出了问题。也许你可以帮忙。下面的代码似乎可以正常获取Atom项,但是查看它们的标题返回为空字符串。

(这是通过http实现的,我不认为这是问题,但当然任何事情都有可能。)

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\n", url)
	fmt.Fprintf(w, "There are %d items in %s\n\n", len(items), url)
	for key, value := range items {
		fmt.Fprintf(w, "%d: %s\n\n", 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
}
英文:

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\n", url)
	fmt.Fprintf(w, "There are %d items in %s\n\n", len(items), url)
	for key, value := range items {
		fmt.Fprintf(w, "%d: %s\n\n", 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
}

答案1

得分: 3

包"go-pkg-rss"在读取响应时存在一些错误。

有很多类似的包,我尝试使用另一个包来复制你的示例(我认为它更简单):

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\n", url)
    fmt.Fprintf(w, "There are %d items in %s\n\n", 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)
}
英文:

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\n", url)
    fmt.Fprintf(w, "There are %d items in %s\n\n", 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

得分: 2

你的代码对我也不起作用。虽然获取的记录数量是正确的,但记录本身没有值。请随时在项目的GitHub页面上提交问题。

另外,请注意你传递项目给HTTP处理程序的方式存在数据竞争问题,因为itemHandler是并发执行的,不知道它是否在HTTP执行for循环的同时被调用。最好使用通道来解决这个问题!

英文:

Your code does not work for me either. While the number of fetched records is correct,
the records itself have no value. Feel free to file an issue on the project's github page.

Also note that your way of transferring the items to the http handler has a data race
as itemHandler gets executed concurrently and it is not known if it is called in
the time the http executes the for loop or not. Better use a channel for that!

答案3

得分: 2

go-pkg-rss使用的xml解析库中引入了一个错误。我已经提交了一个拉取请求,用于修复xml解析器和go-pkg-rss中的问题。拉取请求的链接是:https://github.com/jteeuwen/go-pkg-rss/pull/23

英文:

There was a bug introduced in the xml parsing library go-pkg-rss uses. I've submitted a pull request for both the xml parser and go-pkg-rss to fix the issue. The PR is at: https://github.com/jteeuwen/go-pkg-rss/pull/23

huangapple
  • 本文由 发表于 2013年9月22日 02:12:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/18935911.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定