Skip to content

Instantly share code, notes, and snippets.

@sugyan
Created August 25, 2018 15:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sugyan/894ae790d39cd7fb9927c7f57d316023 to your computer and use it in GitHub Desktop.
Save sugyan/894ae790d39cd7fb9927c7f57d316023 to your computer and use it in GitHub Desktop.
package main
import (
"encoding/xml"
"io"
"log"
"net/http"
"time"
)
func main() {
res, err := http.Get("http://b.hatena.ne.jp/hotentry.rss")
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
if err := do(res.Body); err != nil {
log.Fatal(err)
}
}
func do(r io.Reader) error {
type entry struct {
Title string `xml:"title"`
BookmarkCount int `xml:"bookmarkcount"`
Date time.Time `xml:"date"`
Subjects []string `xml:"subject"`
}
var result struct {
Title string `xml:"channel>title"`
Item []entry `xml:"item"`
}
if err := xml.NewDecoder(r).Decode(&result); err != nil {
return err
}
log.Printf("%v", result.Title)
for _, i := range result.Item {
log.Printf("item: %v", i)
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment