Skip to content

Commit 43e1f3d

Browse files
committed
Add Atom feed examples
1 parent ecaf702 commit 43e1f3d

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

examples/singleapp/atom/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
atom*
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
version: '3'
2+
3+
tasks:
4+
default:
5+
cmds:
6+
- task: run
7+
install-lib:
8+
cmds:
9+
- go install golang.org/x/tools/cmd/goimports@latest
10+
- go install honnef.co/go/tools/cmd/staticcheck@latest
11+
build:
12+
deps: [ install-lib ]
13+
cmds:
14+
- goimports -w .
15+
- go vet
16+
- staticcheck
17+
- go build
18+
run:
19+
deps: [ build ]
20+
cmds:
21+
- ./atom

examples/singleapp/atom/main.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package main
2+
3+
import (
4+
"encoding/xml"
5+
"fmt"
6+
"io"
7+
"net/http"
8+
)
9+
10+
type (
11+
Feed struct {
12+
XMLName xml.Name `xml:"feed"`
13+
Titles []string `xml:"entry>title"`
14+
}
15+
)
16+
17+
const (
18+
URL = "https://devlights.hatenablog.com/feed"
19+
)
20+
21+
func panicOnErr[T any](v T, err error) T {
22+
if err != nil {
23+
panic(err)
24+
}
25+
26+
return v
27+
}
28+
29+
// ブログ [いろいろ備忘録日記](https://devlights.hatenablog.com/) から Atom Feed を取得して表示するサンプルです。
30+
//
31+
// REFERENCES:
32+
// - https://qiita.com/you8/items/e903fd463cf770688e1e
33+
func main() {
34+
fmt.Printf("[URL] %s\n", URL)
35+
36+
resp := panicOnErr(http.Get(URL))
37+
body := panicOnErr(io.ReadAll(resp.Body))
38+
defer resp.Body.Close()
39+
40+
var feed Feed
41+
if err := xml.Unmarshal(body, &feed); err != nil {
42+
panic(err)
43+
}
44+
45+
for i, title := range feed.Titles {
46+
fmt.Printf("[%2d] %s\n", i+1, title)
47+
}
48+
}

0 commit comments

Comments
 (0)