Вопрос

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
}
Это было полезно?

Решение

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)
}

Другие советы

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!

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top