Documentation
¶
Overview ¶
Package iterator provides generic iteration utilities for paginated API responses.
Index ¶
- func Collect[T any](seq iter.Seq2[T, error]) ([]T, error)
- func Filter[T any](seq iter.Seq2[T, error], predicate func(T) bool) iter.Seq2[T, error]
- func Paginate[T any](ctx context.Context, maxResults int, fetcher PageFetcher[T]) iter.Seq2[T, error]
- func PaginateWithOffset[T any](ctx context.Context, limit int, ...) iter.Seq2[T, error]
- func Take[T any](seq iter.Seq2[T, error], n int) iter.Seq2[T, error]
- type PageFetcher
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Collect ¶
Collect gathers all items from an iterator into a slice. Stops and returns error if any iteration fails.
func Paginate ¶
func Paginate[T any](ctx context.Context, maxResults int, fetcher PageFetcher[T]) iter.Seq2[T, error]
Paginate creates an iterator that automatically handles pagination with a required limit. It lazily fetches pages as needed, yielding items one by one up to maxResults.
The fetcher function is called for each page, starting from page 1. Iteration stops when:
- maxResults items have been yielded
- fetcher returns hasMore=false
- an error occurs
- context is cancelled
Example usage:
for game, err := range iterator.Paginate(ctx, 100, fetcher) {
// handle game (max 100 items)
}
func PaginateWithOffset ¶
func PaginateWithOffset[T any]( ctx context.Context, limit int, fetcher func(ctx context.Context, offset, limit int) (items []T, total int, err error), ) iter.Seq2[T, error]
PaginateWithOffset creates an iterator using offset-based pagination. Useful for APIs that use offset/limit instead of page numbers.
Types ¶
Click to show internal directories.
Click to hide internal directories.