-
Notifications
You must be signed in to change notification settings - Fork 18.7k
Description
Proposal Details
The documentation for range currently says:
{{range pipeline}} T1 {{end}}
The value of the pipeline must be an array, slice, map, iter.Seq,
iter.Seq2, integer or channel.
If the value of the pipeline has length zero, nothing is output;
otherwise, dot is set to the successive elements of the array,
slice, or map and T1 is executed. If the value is a map and the
keys are of basic type with a defined order, the elements will be
visited in sorted key order.
The value of the dot for iter.Seq2 is currently undefined. It was discussed in the original proposal, but @rsc concluded that
Regarding the semantics, it seems like we should require that if the iterator yields 2 values, then you have to use the {{range $x, $y := $f}} form.
Unfortunately, the implementation of this vague proposal in Go 1.24 has made a choice making the first value the dot, making it impossible to just switch older slice or map based APIs out with an iterator, which is incredibly unfortunate.
And since the dot for ìter.Seq2 is currently undefined, I suggest defining this for Go 1.25 and make it compatible with slices and maps, which is by far the most common data structures to range on in Go templates.
I have seen arguments about this somehow not working if you want to have error as the second value in an iterator, but you could then use {{range $x, $err := $f}} (which you should, according to the above). Error handling isn't a first citizen in the Go template design, not sure why this should now suddenly drive the requirements.