golang模板中如何使用continue和break关键字?
How to use continue and break keywords in golang templates?
例如:
{{range .Users}}
{{if .IsAdmin}}
{{/* How to use "break" or "continue"? */}}
{{end}}
{{end}}
模板中 "break" 或 "continue" 的文档在 golang.org
中不可用
它们不存在,因此未记录在案。
为了确保 - 检查 text/template
词法分析器的测试:https://github.com/golang/go/blob/master/src/text/template/parse/lex_test.go
break
和 continue
语句是 Go 1.10 中 text/template
和 html/template
的一部分(在撰写本文时处于 Beta 阶段)。来自 release notes:
The new actions {{break}}
and {{continue}}
break out of the innermost
{{range ...}}
loop, like the corresponding Go statements.
Go 的早期版本(1.10 之前)不 支持 break
或 continue
语句。
查看测试版文档,您可以看到 lexer, the new nodes like ContinueNode
in Parser 中的新 itemContinue
和 itemBreak
项目遵循代码。
您可以使用变量添加处理中检查并跳过循环剩余部分的任何现有检查。
// method 1
{{ $dothing := false }}
{{ range $i, $A := .List }}
{{ if $A.Check }}
{{ $dothing = true }}
{{end}}
{{ end }}
{{ if $dothing }}
// do thing
{{ end }}
// method 2
{{ $already := false }}
{{ range $i, $A := .List }}
{{ if $already }}
{{ else }}
{{ if $A.Check }}
{{ $already = true }}
// do thing
{{ end }}
{{ end }}
{{ end }}
Go101 does mention(2021 年 5 月,4 年多之后):
Since Go 1.18, "break" and "continue" might be supported in range loops of Go templates.
注意:Go 1.18 应在 2022 年第一季度发布。
这将解决 issue 20531 text/template:添加 break
和 continue
支持。
目前在 CL 321491 中实现:html/template
、text/template
:为 range
循环实现 break
和 continue
。
这项工作目前仍在进行中(2021 年第一季度)
2021 年 9 月更新:confirmed, with commit d0dd26a
html/template
, text/template
: implement break
and continue
for range
loops
break
and continue
for range loops was accepted as a proposal in June 2017.
It was implemented in CL 66410 (Oct 2017)
but then rolled back in CL 92155 (Feb 2018) because html/template
changes had not been implemented.
This CL reimplements break and continue in text/template
and then adds support for them in html/template as well.
例如:
{{range .Users}}
{{if .IsAdmin}}
{{/* How to use "break" or "continue"? */}}
{{end}}
{{end}}
模板中 "break" 或 "continue" 的文档在 golang.org
中不可用它们不存在,因此未记录在案。
为了确保 - 检查 text/template
词法分析器的测试:https://github.com/golang/go/blob/master/src/text/template/parse/lex_test.go
break
和 continue
语句是 Go 1.10 中 text/template
和 html/template
的一部分(在撰写本文时处于 Beta 阶段)。来自 release notes:
The new actions
{{break}}
and{{continue}}
break out of the innermost{{range ...}}
loop, like the corresponding Go statements.
Go 的早期版本(1.10 之前)不 支持 break
或 continue
语句。
查看测试版文档,您可以看到 lexer, the new nodes like ContinueNode
in Parser 中的新 itemContinue
和 itemBreak
项目遵循代码。
您可以使用变量添加处理中检查并跳过循环剩余部分的任何现有检查。
// method 1
{{ $dothing := false }}
{{ range $i, $A := .List }}
{{ if $A.Check }}
{{ $dothing = true }}
{{end}}
{{ end }}
{{ if $dothing }}
// do thing
{{ end }}
// method 2
{{ $already := false }}
{{ range $i, $A := .List }}
{{ if $already }}
{{ else }}
{{ if $A.Check }}
{{ $already = true }}
// do thing
{{ end }}
{{ end }}
{{ end }}
Go101 does mention(2021 年 5 月,4 年多之后):
Since Go 1.18, "break" and "continue" might be supported in range loops of Go templates.
注意:Go 1.18 应在 2022 年第一季度发布。
这将解决 issue 20531 text/template:添加 break
和 continue
支持。
目前在 CL 321491 中实现:html/template
、text/template
:为 range
循环实现 break
和 continue
。
这项工作目前仍在进行中(2021 年第一季度)
2021 年 9 月更新:confirmed, with commit d0dd26a
html/template
,text/template
: implementbreak
andcontinue
forrange
loops
break
andcontinue
for range loops was accepted as a proposal in June 2017.
It was implemented in CL 66410 (Oct 2017)
but then rolled back in CL 92155 (Feb 2018) becausehtml/template
changes had not been implemented.This CL reimplements break and continue in
text/template
and then adds support for them in html/template as well.