Iterators and the process of iteration

Iteration is the method of looking through a group of data, usually a list, in order to retrieve information from said list. Go has a bunch of different iterator patterns, all with benefits and drawbacks:

Iterator Benefit Drawback
for loop Simplest implementation No default concurrency.
Iterator function with a callback Simple implementation Unconventional styling for Go; difficult to read.
Channels Simple implementation More expensive computationally than some other iterators (with a marginal cost difference). The only iterator that is naturally concurrent.
Stateful iterators Difficult implementation A nice caller interface. Useful for complex iterators (commonly used in the standard library). ...

Get Hands-On High Performance with Go now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.