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.
Or read this on Hacker News