iterator

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 26, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package iterator provides generic iteration utilities for paginated API responses.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Collect

func Collect[T any](seq iter.Seq2[T, error]) ([]T, error)

Collect gathers all items from an iterator into a slice. Stops and returns error if any iteration fails.

func Filter

func Filter[T any](seq iter.Seq2[T, error], predicate func(T) bool) iter.Seq2[T, error]

Filter returns an iterator that only yields items matching the predicate.

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.

func Take

func Take[T any](seq iter.Seq2[T, error], n int) iter.Seq2[T, error]

Take returns an iterator that yields at most n items.

Types

type PageFetcher

type PageFetcher[T any] func(ctx context.Context, page int) (items []T, hasMore bool, err error)

PageFetcher is a function that fetches a page of results. It returns the items, whether there are more pages, and any error.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL