Can't range over iter.Seq in text/template (go 1.22/dev)

75 views
Skip to first unread message

Rory Campbell-Lange

unread,
Jul 6, 2024, 5:21:16 PM (2 days ago) Jul 6
to golang-nuts
I don't seem to be able to range over an iter.Seq in a template.

error: executing "test" at <.>: range can't iterate over 0x5103c0

resulter := func(yield func(int) bool) {
for _, v := range []int{1, 2, 3} {
if !yield(v) {
return
}
}
}

tpl := `{{ range $aa := . }}{{ $aa }} {{ end }}`

tplParsed := template.Must(template.New("test").Parse(tpl))
err := tplParsed.Execute(os.Stdout, resulter)
if err != nil {
fmt.Println(err)
}

https://go.dev/play/p/f1mrGFnfiUa?v=gotip

https://pkg.go.dev/text/template says:
{{range pipeline}} T1 {{end}}
The value of the pipeline must be an array, slice, map, or channel

...is an iter.Seq or iter.Seq2 not intended to be added there?

Thanks,
Rory

Ian Lance Taylor

unread,
Jul 6, 2024, 5:54:33 PM (2 days ago) Jul 6
to Rory Campbell-Lange, golang-nuts
On Sat, Jul 6, 2024 at 2:21 PM Rory Campbell-Lange
<ro...@campbell-lange.net> wrote:
>
> I don't seem to be able to range over an iter.Seq in a template.

...

> ...is an iter.Seq or iter.Seq2 not intended to be added there?


Yes, this is https://go.dev/issue/66107.

Ian

Rory Campbell-Lange

unread,
Jul 7, 2024, 3:01:06 AM (21 hours ago) Jul 7
to Ian Lance Taylor, golang-nuts
Apologies, I didn't spot that.
Reply all
Reply to author
Forward
0 new messages