Skip to content

Iterators: resources

Level: 201 · anyone who wants the primary sources behind this chapter, and to know which books predate it

One line: Range over a function is from August 2024, so the specification, the iter package documentation, one blog post, one wiki page and the proposals are the sources; every book on the shelf was written before it, and their for-range chapters need reading with that in mind.

Primary sources

Books

None of these covers range over a function; they are listed for the parts they do cover — the for-range statement as it was, closures, and the deferred calls an iterator's cleanup is made of.

  • Jon Bodner, Learning Go, 2nd ed. (O'Reilly, 2024) — chapter 4, "Blocks, Shadows, and Control Structures": "The for-range Statement" (with "Iterating over maps", the map-order randomization, and "Iterating over strings") and "Choosing the Right for Statement"; chapter 5, "Functions": "Closures", "Passing Functions as Parameters" and "Returning Functions from Functions"; chapter 7, "Types, Methods, and Interfaces": "Function Types Are a Bridge to Interfaces".
  • Alan A. A. Donovan and Brian W. Kernighan, The Go Programming Language (Addison-Wesley, 2015) — chapter 5, "Functions": 5.5 "Function Values", 5.6 "Anonymous Functions", 5.8 "Deferred Function Calls", 5.9 "Panic" and 5.10 "Recover".
  • Adam Freeman, Pro Go (Apress, 2022) — chapter 6, "Flow Control": "Enumerating Sequences"; chapter 9, "Using Function Types": "Using Functions as Arguments", "Using Functions as Results" and "Understanding Function Closure".
  • Miki Tebeka, Effective Go Recipes (Pragmatic, 2024) — chapter 5, "Working with Functions": recipe 28 "Using Closures to Provide Options with Defaults" and recipe 29 "Passing Notifications with Functions" — callbacks, which is what yield is.
  • Go in Practice, 2nd ed. (Manning, 2025) — chapter 3, "Structs, interfaces, and generics", the passage on functional style and passing functions as values; its 2025 date notwithstanding, it does not use iter.
  • Katherine Cox-Buday, Concurrency in Go (O'Reilly, 2017) — chapter 4, "Concurrency Patterns in Go": the "Pipelines" section and its generator stage, the goroutine-and-channel shape that An iterator is lazy compares with.

Not on the shelf

  • Teiva Harsanyi, 100 Go Mistakes and How to Avoid Them (Manning, 2022) — 100go.co ↗; predates iterators, and its loop and closure mistakes still apply to the loop body.
  • Bartłomiej Płotka, Efficient Go (O'Reilly, 2022) — the publisher's page ↗; for the benchmark discipline the planned iterator benchmarks would follow.

Talks and videos

  • Ian Lance Taylor, "Range Over Function Types", GopherCon 2024 (Chicago) — the talk the blog post ↗ of the same title was written from; the post is the citable form.
  • Takuya Ueda, "What is the iterator and how does it build an ecosystem in Go?", GopherDay Taiwan 2024 — session page ↗.

Read with care

  • Any statement that for-range works only on built-in types — Learning Go's chapter 4 says a for-range loop iterates only the built-in compound types and user-defined types based on them, true for its Go 1.20 and wrong since 1.23.
  • Pre-1.23 "iterator pattern" advice: a Next() bool / Value() pair on a struct, a channel fed by a goroutine, or a callback with no bool result. All still work; the first two are what iter.Pull and a Seq replace, and the third cannot be ranged over (yield func does not return bool).
  • Articles from the Go 1.22 experiment (GOEXPERIMENT=rangefunc, late 2023 to mid-2024) — the semantics shipped as designed, but package names, the xiter adapters and some runtime messages in those posts differ from what 1.23 shipped.
  • Claims that strings.Lines or SplitSeq can be ranged twice — they are documented single-use, and slices and maps have iterators prints the empty second pass.
  • The defer-in-the-body rule stated without qualification — correct as the wiki states it, and the fence in break makes yield return false shows go1.25.5 breaking it for an inlined function literal.

Checked 2026-09-23.