Ranging over Functions in Go 1.23
Go 1.23 introduces ranging over functions, allowing iteration over custom data structures with for-range loops, simplifying iterator creation, supporting early termination, and enhancing code clarity through various examples.
Read original articleGo 1.23 introduces a significant feature: ranging over functions, enhancing the language's iteration capabilities. This feature allows developers to use the familiar for-range loop not only with built-in types like slices and maps but also with custom data structures. The article discusses how this new functionality simplifies iteration over generic containers, which previously required custom iteration methods. A key example is the implementation of an associative list (AssocList) that can now be iterated over using a standard for-range loop. The new method, All, returns a function that accepts a yield function, enabling the iteration process. The article also explains the mechanics behind this feature, detailing how the compiler transforms the for-range loop into calls to the yield function, allowing for early termination of iteration when necessary. Additionally, it covers more complex iteration patterns, such as recursive iterators for binary trees and examples of using the new feature with existing types like bufio.Scanner. Overall, this enhancement streamlines the process of creating and using iterators in Go, promoting a more idiomatic approach to iteration.
- Go 1.23 introduces ranging over functions, allowing iteration over custom data structures.
- The new feature simplifies the creation of iterators for generic containers.
- The for-range loop can now be used with functions that yield values, enhancing code clarity.
- Early termination of iteration is supported, improving control over the iteration process.
- The article provides examples, including associative lists and binary trees, demonstrating the new functionality.
Related
Go Range Iterators Demystified
The Go 1.23 release introduces range iterators for custom collection types, offering flexibility for iteration beyond maps and slices. These iterators support various loop forms and enable powerful iteration scenarios.
First impressions of Go 1.23's range-over-func feature
The author shares positive experiences with Go 1.23's range-over-func feature, initially skeptical but finding it easy to use. Successful adaptation in their project Kivik disproved initial concerns, highlighting benefits for codebase improvement.
Go 1.23: Interactive release notes
Go 1.23 introduces interactive release notes with enhanced sequence iteration using iterators like Seq and Seq2. It formalizes iterator types, improves time.Timer behavior, and offers memory optimization tools.
Go 1.23
Go 1.23 has been released with enhancements to the toolchain, runtime, and libraries, including the "range-over-func" feature, opt-in telemetry, and updates to the standard library for improved performance.
Fun with Go Iterators
Go version 1.23 introduced iterator support, prompting the author to create a wrapper for easier method chaining. This abstraction enhances readability for operations like reverse, map, and filter.
Related
Go Range Iterators Demystified
The Go 1.23 release introduces range iterators for custom collection types, offering flexibility for iteration beyond maps and slices. These iterators support various loop forms and enable powerful iteration scenarios.
First impressions of Go 1.23's range-over-func feature
The author shares positive experiences with Go 1.23's range-over-func feature, initially skeptical but finding it easy to use. Successful adaptation in their project Kivik disproved initial concerns, highlighting benefits for codebase improvement.
Go 1.23: Interactive release notes
Go 1.23 introduces interactive release notes with enhanced sequence iteration using iterators like Seq and Seq2. It formalizes iterator types, improves time.Timer behavior, and offers memory optimization tools.
Go 1.23
Go 1.23 has been released with enhancements to the toolchain, runtime, and libraries, including the "range-over-func" feature, opt-in telemetry, and updates to the standard library for improved performance.
Fun with Go Iterators
Go version 1.23 introduced iterator support, prompting the author to create a wrapper for easier method chaining. This abstraction enhances readability for operations like reverse, map, and filter.