digitalocean / digitalocean/godo

Include more modern example of handling pagination

Open
#861 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
1.5k
Forks
389
Avg merge
1d 23h
Merged PRs (30d)
15

Description

Consider the inclusion of something like the following in the package's README, as an alternative to the existing recommended boilerplate for handling paginated responses.

Generics were introduced in version `1.18`, and the `iter.Seq` function in `1.23`. Many existing and potential users of godo would benefit from an example utlising these packages:

```go
func Unpaginate[T any](ctx context.Context, f func(ctx context.Context, opt *godo.ListOptions) ([]T, *godo.Response, error), opt godo.ListOptions) iter.Seq2[T, error] {
return func(yield func(T, error) bool) {
var buffer T
for {
items, resp, err := f(ctx, &opt)
if err != nil {
yield(buffer, err)
return
}
for _, item := range items {
if !yield(item, nil) {
return
}
}
if resp.Links == nil || resp.Links.IsLastPage() {
return
}

page, err := resp.Links.CurrentPage()
if err != nil {
yield(buffer, err)
return
}

// set the page we want for the next request
opt.Page = page + 1
}
}
}
```

Contributor guide

Open the contributing guide

Research direction

Start by finding the existing pagination boilerplate in the package README and compare it with the proposed Go generics and iter.Seq example. Done means the README includes a modern alternative for handling paginated responses, with the relevant Go version context and example presented clearly.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
api, documentation
Issue type
Documentation
Difficulty
1/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.