- Introduction: Using Go to write [Dolt], the world's first version-controlled SQL database. In Go 1.23, you can use the
rangekeyword to iterate over custom collection types. Need to install Go 1.23 RC or setGOEXPERIMENT=rangefuncin Go 1.22. - What are range iterators: Explanation from experiment documentation was confusing. Release notes summarize the feature better. Range iterators are function types used with
rangekeyword starting in Go 1.23. There are three types:func(func() bool),func(func(K) bool),func(func(K, V) bool), corresponding to differentrangeloop forms. Simple examples shown. - Types of range iterators: Three types with 0, 1, or 2 arguments. Examples of each type are provided, including functions that take no arguments, one argument, and two arguments.
- What does
yield()do: Theyieldfunction in a range iterator invokes the loop body. The Go compiler convertsrangeloops with iterators into function calls. Not checking the result ofyield()can cause a panic. You can callyield()multiple times or with different arguments. - Use cases: Main use case is to use
rangekeyword to iterate over custom collection types. Examples include iterating over all elements, filtering values using a predicate function, and iterating while handling errors. Also shows how to handle sentinel errors and convert traditional iterators to range-compatible iterators. PullandPull2: Go authors includedPullandPull2convenience functions to transform range iterator functions into traditional iterators. Demonstrated with an example of wrapping and unwrapping an iterator.- Readability and convention considerations: Go convention for
rangeis different for range iterators. Single-var version may work differently. This is a new feature with no established conventions yet. - Conclusion: All examples are available here. Join Discord to discuss Go range iterators or Dolt.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用。你还可以使用@来通知其他用户。