如何在golang模板中使用continue和break关键字?

emu*_*tol 4 go-templates

例如:

{{range .Users}}
    {{if .IsAdmin}}
        {{/* How to use "break" or "continue"? */}}
    {{end}}
{{end}}
Run Code Online (Sandbox Code Playgroud)

golang.org中没有模板中“break”或“continue”的文档

Von*_*onC 6

Go101 确实提到(2021 年 5 月,4 年多后):

从 Go 1.18 开始,Go 模板的范围循环可能支持“break”和“continue”。

注意:Go 1.18应该在 2022 年第一季度发布。

这将解决问题 20531文本/模板:添加breakcontinue支持。
目前已在CL 321491中实现:html/template, text/template: 实现breakcontinueforrange循环。

目前该工作仍在进行中(2021 年第一季度)

2021 年 9 月更新:已确认提交 d0dd26a

html/template, text/template:break实现continueforrange循环

breakfor continuerange 循环于 2017 年 6 月被接受为提案。它在CL 66410(2017 年 10 月)
中实施,但随后在CL 92155(2018 年 2 月) 中回滚,因为更改尚未实施。
html/template

这个 CL 重新实现了 break 和 continue text/template,然后也在 html/template 中添加了对它们的支持。