From 8ef023ef3f1da3f16e9cc32ca6749ce780ea80ed Mon Sep 17 00:00:00 2001 From: Vladimir Varankin Date: Thu, 10 Jul 2025 16:38:27 +0200 Subject: [PATCH] iter: document that calling yield after terminated range loop causes panic The existing documentation isn't clear about the behaviour of when the iterator function ignores the value returned from the yield function. The changes here update the documentation, to explicitly explain that. --- src/iter/iter.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/iter/iter.go b/src/iter/iter.go index 3eaeb9e1fd6f1f..23fd071e32877c 100644 --- a/src/iter/iter.go +++ b/src/iter/iter.go @@ -26,7 +26,7 @@ Seq2 represents a sequence of paired values, conventionally key-value or index-value pairs. Yield returns true if the iterator should continue with the next -element in the sequence, false if it should stop. +element in the sequence, false if it must stop. For instance, [maps.Keys] returns an iterator that produces the sequence of keys of the map m, implemented as follows: @@ -51,6 +51,9 @@ Iterator functions are most often called by a [range loop], as in: } } +If a [range loop] terminates, e.g. by a break statement, a subsequent call +to yield inside the iterator causes a runtime panic. + # Naming Conventions Iterator functions and methods are named for the sequence being walked: