Get the latest tech news

Go range iterators demystified


How the new range iterators in the upcoming Go 1.23 release work, with examples

This uses the 2-parameter variation of a range loop, which in the case of a slice or a map returns the index and element or the key and value, respectively. But because it's both easy to do and useful, I predict this use case will emerge as a common pattern for custom collection types. You also can use the same strategy, minus the error handling, to convert any traditional iterator object to one compatible with the range keyword using this technique.

Get the Android app

Or read this on Hacker News

Read more on:

Photo of Range Iterators

Range Iterators