snoobert/examples/get-top-posts/main.go
Vartan Benohanian 2a1806ec33 Revamp listing decoding, include after/before anchors in response
Now, instead of returning an object containing a list of results + the
anchors, we return just the list. The anchors are available in the
response object. Much cleaner this way in my opinion

go-github and godo do it this way too. They include some meta
information in the returned response objects

Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
2020-08-29 14:23:37 -04:00

55 lines
1 KiB
Go

package main
import (
"context"
"fmt"
"log"
"github.com/vartanbeno/go-reddit/reddit"
)
var ctx = context.Background()
func main() {
if err := run(); err != nil {
log.Fatal(err)
}
}
func run() (err error) {
// Let's get the top 200 posts of r/golang.
// Reddit returns a maximum of 100 posts at a time,
// so we'll need to separate this into 2 requests.
posts, resp, err := reddit.DefaultClient.Subreddit.TopPosts(ctx, "golang", &reddit.ListPostOptions{
ListOptions: reddit.ListOptions{
Limit: 100,
},
Time: "all",
})
if err != nil {
return
}
for _, post := range posts {
fmt.Println(post.Title)
}
// The After option sets the id of an item that Reddit
// will use as an anchor point for the returned listing.
posts, _, err = reddit.DefaultClient.Subreddit.TopPosts(ctx, "golang", &reddit.ListPostOptions{
ListOptions: reddit.ListOptions{
Limit: 100,
After: resp.After,
},
Time: "all",
})
if err != nil {
return
}
for _, post := range posts {
fmt.Println(post.Title)
}
return
}