A quick look at
range
-aware custom iterators.
Since Go 1.23, you can add support for the range
construct to
user-defined types. This provides a standard interface for iteration,
which should make it easier for those experimenting with third-party
containers.
To start off, there’s been nice feature since 1.22 more or less on the same topic:
A terser syntax for iterating over integers in the range [0, n)
.
Similar to APL’s Iota
primitive.
Example: Iterating from [0, 10)
for i := range 10 {
fmt.Printf("i = %v\n", i)
}
Since the loop variable is optional, if we simply want to execute an
action, say 3
times, the code boils down to:
for range 3 {
fmt.Println("some-action")
}