Question about text/template handling of iter.Seq2

170 views
Skip to first unread message

Robert Johnstone

unread,
Aug 9, 2026, 7:57:54 PM (7 days ago) Aug 9
to golang-nuts

I'm seeing some unexpected behaviour when running templates using text/template. Specifically, when ranging over iter.Seq2 the cursor follows the first parameter, not the second as for example when ranging over a slice. Is this the expected behaviour?

https://go.dev/play/p/5JK7kIhXGxs

Jason E. Aten

unread,
Aug 9, 2026, 11:26:30 PM (7 days ago) Aug 9
to golang-nuts
maybe you want the `range $index, $element := pipeline` form mentioned in https://pkg.go.dev/text/template#hdr-Variables

https://go.dev/play/p/AgfUpwuhqFE

Brian Candler

unread,
Aug 10, 2026, 3:13:08 PM (6 days ago) Aug 10
to golang-nuts
It also says at the link Jason provided:

"Note that if there is only one variable, it is assigned the element; this is opposite to the convention in Go range clauses."

Hence it is expected behaviour, if somewhat surprising.

Brian Candler

unread,
Aug 10, 2026, 3:35:48 PM (6 days ago) Aug 10
to golang-nuts
(except I see your point, why does iter.Seq2 behave differently?)

Jan Mercl

unread,
Aug 10, 2026, 4:07:51 PM (6 days ago) Aug 10
to Brian Candler, golang-nuts
On Mon, Aug 10, 2026 at 9:36 PM 'Brian Candler' via golang-nuts
<golan...@googlegroups.com> wrote:

> (except I see your point, why does iter.Seq2 behave differently?)

(I suspect someone's convenience won over everyone's consistency)

qiu laidongfeng

unread,
Aug 12, 2026, 12:09:11 AM (5 days ago) Aug 12
to golang-nuts
See https://github.com/golang/go/issues/66107#issuecomment-1978357619 and https://github.com/golang/go/issues/73282#issuecomment-2906937179
Regarding the background of current behavior to answer this question.

However, it is indeed unfortunate, Maybe https://github.com/golang/go/discussions/80580
Can better avoid such situations in the future.
Reply all
Reply to author
Forward
0 new messages