dcreager.net

Range over function

Go 1.23 introduced “range over function types”, which lets you implement custom logic for ranging over values in a for statement.

Announcement post [go.dev]

They're an interesting take on iterators (specifically “push” iterators) that are unlike most other languages. They work even in the presence of return, break, continue, goto, and defer inside the loop body.

mcyoung has a good writeup of the code rewrite that's needed to make that work.

Go's Weird Little Iterators [mcyoung.xyz]

That includes a link to the behemoth of a comment from Russ Cox and David Chase in the rewrite implementation.

Package rangefunc [cs.opensource.google]

» Languages » Go