Starting with version 1.23, Go has added support for iterators, which lets us range over pretty much anything! |
|
|
|
|
|
Let’s look at the |
|
|
|
|
|
All returns an iterator, which in Go is a function with a special signature. |
|
The iterator function takes another function as
a parameter, called |
|
Iteration doesn’t require an underlying data structure,
and doesn’t even have to be finite! Here’s a function
returning an iterator over Fibonacci numbers: it keeps
running as long as |
|
|
|
|
|
Since |
|
Packages like slices have
a number of useful functions to work with iterators.
For example, |
|
|
|
Once the loop hits |
|
|
Next example: Errors.