Golang模板 – 更新和打印计数变量

huangapple go评论124阅读模式
英文:

Golang Templates - Update and Print Counter Variable

问题

我正在尝试在Go模板中通过嵌套的for循环来更新和打印一个计数器变量。

想打印任何一个数据结构的索引,我也不想打印每个数据结构中的所有对象,我希望这个计数器变量只在等式为真时递增。

我在这里做错了什么?

Go Playground链接:https://play.golang.org/p/RsuEk1PqZ7a

  1. type a struct {
  2. Numbers []string
  3. Letters []string
  4. }
  5. var data = &a{
  6. Numbers: []string{"one", "two"},
  7. Letters: []string{"a","b","b", "c"},
  8. }
  9. var tmplSrc = `start
  10. {{with $i := 0}}
  11. {{range $number := .Numbers}}
  12. {{range $letter := .Letters}}
  13. {{if eq $letter "b"}}
  14. {{$i = add $i 1}}
  15. {{$i}}
  16. {{end}}
  17. {{end}}
  18. {{end}}
  19. {{end}}
  20. fin
  21. `
  22. func main() {
  23. funcMap := template.FuncMap{
  24. "add": func(a int, b int) int {
  25. return a + b
  26. },
  27. }
  28. tmpl := template.Must(template.New("test").Funcs(funcMap).Parse(tmplSrc))
  29. tmpl.Execute(os.Stdout, data)
  30. }
英文:

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

  1. type a struct {
  2. Numbers []string
  3. Letters []string
  4. }
  5. var data = &a{
  6. Numbers: []string{"one", "two"},
  7. Letters: []string{"a","b","b", "c"},
  8. }
  9. var tmplSrc = `start
  10. {{with $i := 0}}
  11. {{range $number := .Numbers}}
  12. {{range $letter := .Letters}}
  13. {{if eq $letter "b"}}
  14. {{$i = add $i 1}}
  15. {{$i}}
  16. {{end}}
  17. {{end}}
  18. {{end}}
  19. {{end}}
  20. fin
  21. `
  22. func main() {
  23. funcMap := template.FuncMap{
  24. "add": func(a int, b int) int {
  25. return a + b
  26. },
  27. }
  28. tmpl := template.Must(template.New("test").Funcs(funcMap).Parse(tmplSrc))
  29. tmpl.Execute(os.Stdout, data)
  30. }

答案1

得分: 1

从tmpl.Execute执行返回的错误提示了问题所在:

> test:3:21: 在<.Numbers>中执行“test”时,无法评估类型为int的字段Numbers

始终处理错误!

问题在于{{with $i := 0}}.设置为0。模板期望.是模板的数据参数。通过使用$来引用数据参数来修复。对于.Letters也需要进行类似的更改,因为{{range}}也会设置.

  1. {{with $i := 1}}
  2. {{range $number := $.Numbers}}
  3. {{range $letter := $.Letters}}
  4. {{if eq $letter "b"}}
  5. {{$i = add $i 1}}
  6. {{$i}}
  7. {{end}}
  8. {{end}}
  9. {{end}}
  10. {{end}}

我使用{{with $i := 1}}来匹配playground中的代码。问题中使用了{{with $i := 0}}。问题中的代码引入了另一个问题:{{with}}的内容被跳过,因为条件求值为false(在模板条件中,0为false)。通过删除{{with}}指令来修复。它是不需要的。

  1. {{$i := 0}}
  2. {{range $number := $.Numbers}}
  3. {{range $letter := $.Letters}}
  4. {{if eq $letter "b"}}
  5. {{$i = add $i 1}}
  6. {{$i}}
  7. {{end}}
  8. {{end}}
  9. {{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 ..

  1. {{with $i := 1}}
  2. {{range $number := $.Numbers}}
  3. {{range $letter := $.Letters}}
  4. {{if eq $letter &quot;b&quot;}}
  5. {{$i = add $i 1}}
  6. {{$i}}
  7. {{end}}
  8. {{end}}
  9. {{end}}
  10. {{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.

  1. {{$i := 0}}
  2. {{range $number := $.Numbers}}
  3. {{range $letter := $.Letters}}
  4. {{if eq $letter &quot;b&quot;}}
  5. {{$i = add $i 1}}
  6. {{$i}}
  7. {{end}}
  8. {{end}}
  9. {{end}}

huangapple
  • 本文由 发表于 2021年10月27日 12:17:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/69732718.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定