英文:
Golang Templates - Update and Print Counter Variable
问题
我正在尝试在Go模板中通过嵌套的for循环来更新和打印一个计数器变量。
我不想打印任何一个数据结构的索引,我也不想打印每个数据结构中的所有对象,我希望这个计数器变量只在等式为真时递增。
我在这里做错了什么?
Go Playground链接:https://play.golang.org/p/RsuEk1PqZ7a
type a struct {
Numbers []string
Letters []string
}
var data = &a{
Numbers: []string{"one", "two"},
Letters: []string{"a","b","b", "c"},
}
var tmplSrc = `start
{{with $i := 0}}
{{range $number := .Numbers}}
{{range $letter := .Letters}}
{{if eq $letter "b"}}
{{$i = add $i 1}}
{{$i}}
{{end}}
{{end}}
{{end}}
{{end}}
fin
`
func main() {
funcMap := template.FuncMap{
"add": func(a int, b int) int {
return a + b
},
}
tmpl := template.Must(template.New("test").Funcs(funcMap).Parse(tmplSrc))
tmpl.Execute(os.Stdout, data)
}
英文:
I am trying to update and print a counter variable inside of a Go template when iterating through a nested for loop.
I am not trying to print the index of either of my data structures as I loop through. I am not going to print every object in each data structure, and I want this counter variable to only increment when an equality statement is true.
What am I doing wrong here?
Go Playground link: https://play.golang.org/p/RsuEk1PqZ7a
type a struct {
Numbers []string
Letters []string
}
var data = &a{
Numbers: []string{"one", "two"},
Letters: []string{"a","b","b", "c"},
}
var tmplSrc = `start
{{with $i := 0}}
{{range $number := .Numbers}}
{{range $letter := .Letters}}
{{if eq $letter "b"}}
{{$i = add $i 1}}
{{$i}}
{{end}}
{{end}}
{{end}}
{{end}}
fin
`
func main() {
funcMap := template.FuncMap{
"add": func(a int, b int) int {
return a + b
},
}
tmpl := template.Must(template.New("test").Funcs(funcMap).Parse(tmplSrc))
tmpl.Execute(os.Stdout, data)
}
答案1
得分: 1
从tmpl.Execute执行返回的错误提示了问题所在:
> test:3:21: 在<.Numbers>中执行“test”时,无法评估类型为int的字段Numbers
始终处理错误!
问题在于{{with $i := 0}}将.设置为0。模板期望.是模板的数据参数。通过使用$来引用数据参数来修复。对于.Letters也需要进行类似的更改,因为{{range}}也会设置.。
{{with $i := 1}}
{{range $number := $.Numbers}}
{{range $letter := $.Letters}}
{{if eq $letter "b"}}
{{$i = add $i 1}}
{{$i}}
{{end}}
{{end}}
{{end}}
{{end}}
我使用{{with $i := 1}}来匹配playground中的代码。问题中使用了{{with $i := 0}}。问题中的代码引入了另一个问题:{{with}}的内容被跳过,因为条件求值为false(在模板条件中,0为false)。通过删除{{with}}指令来修复。它是不需要的。
{{$i := 0}}
{{range $number := $.Numbers}}
{{range $letter := $.Letters}}
{{if eq $letter "b"}}
{{$i = add $i 1}}
{{$i}}
{{end}}
{{end}}
{{end}}
英文:
The error returned from tmpl.Execute execute hints at the problem:
> test:3:21: executing "test" at <.Numbers>: can't evaluate field Numbers in type int
Always handle errors!
The problem is that {{with $i := 0}} sets . to the 0. The template expects . to be the data argument to the template. Fix by using $ to refer to the data argument. A similar change is required for .Letters because {{range}} also sets ..
{{with $i := 1}}
{{range $number := $.Numbers}}
{{range $letter := $.Letters}}
{{if eq $letter "b"}}
{{$i = add $i 1}}
{{$i}}
{{end}}
{{end}}
{{end}}
{{end}}
I used {{with $i := 1}} to match the code in the playground. The question uses {{with $i := 0}}. The code in the question introduces yet another problem: the contents of the {{with}} is skipped because the condition evaluates to false (0 is false in template conditions). Fix by removing the {{with}} directive. It's not needed.
{{$i := 0}}
{{range $number := $.Numbers}}
{{range $letter := $.Letters}}
{{if eq $letter "b"}}
{{$i = add $i 1}}
{{$i}}
{{end}}
{{end}}
{{end}}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论