Go Iterators: A Tale of Purity and Impurity

2025-05-31

Go 1.23 standardized iterators, powerful functions that can be closures. However, the official documentation's classification of iterators is ambiguous. The author proposes a clearer distinction between 'pure' and 'impure' iterators: 'pure' iterators restart each time, while 'impure' iterators may retain state. The article explores various iterator types and the trade-offs between performance and consistency, concluding that Go's iterator landscape is still evolving, with conventions and terminology needing further refinement.

Read more
Development

The Performance Cost of Abusing Go's panic/recover

2025-03-04

This article benchmarks the performance difference between using Go's `panic` and `recover` for array iteration versus a traditional loop. The results demonstrate a significant performance penalty for abusing `panic`/`recover` for control flow in smaller datasets. This is attributed to the inhibition of compiler optimizations such as inlining and bounds check elimination. While `panic`/`recover` can offer efficiency gains in handling internal errors, the author cautions against overuse and stresses the importance of keeping such mechanisms internal to a package, away from public APIs.

Read more
Development