Created
August 25, 2018 15:42
-
-
Save sugyan/894ae790d39cd7fb9927c7f57d316023 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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