Get the latest tech news

Writing and testing a paginated API iterator in Go


ublished on 2024-12-19 Reading time estimated to 8 minutes Go 1.23, amongst other features, brought Iterators to the standard library. Iterators are basically a way to make the range operator work on functions implementing a specific interface.

As you can see, the iterator version is slightly more verbose, but it's more reusable and has the benefit of isolating the pagination logic from the rest of the code. This lets us pass the parameters that will be available in the scope of the iterator: the context, the GitHub client, the target user, and the additional options. First, the iterator is initialized by calling IterateRepositoriesByUser with its expected parameters, including the mock client we created previously.

Get the Android app

Or read this on Hacker News

Read more on:

Photo of API

API

Related news:

News photo

OpenAI brings its o1 reasoning model to its API — for certain developers

News photo

Lambda launches ‘inference-as-a-service’ API claiming lowest costs in AI industry

News photo

Upvest, a stock trading API used by N26, Revolut and others, raises $105 million