Skip to content

proposal: text/template: make the dot in range for iter.Seq2 match ranging over slices and maps #73861

@bep

Description

@bep

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    LanguageProposalIssues describing a requested change to the Go language specification.Proposal

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions