Go模板表达式

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

Go template expression

问题

  1. {{$Total := 0.00}}
  2. {{ range .Shoes }}
  3. {{ if .Quantity }}
  4. {{ $Total := add $Total (mul .Price .Quantity)}}
  5. {{ end }}
  6. {{ end }}
  7. {{printf "%.2f" $Total}}

函数"add"未定义

总价 * 数量 = 最终价格

英文:
  1. {{$Total := 0.00}}
  2. {{ range .Shoes }}
  3. {{ if .Quantity }}
  4. {{ $Total := add $Total (mul .Price .Quantity)}}
  5. {{ end }}
  6. {{ end }}
  7. {{printf "%.2f" $Total}}

function "add" not defined

Total price * Quantity = Final Price

答案1

得分: 3

  1. package main
  2. import (
  3. "bytes"
  4. "fmt"
  5. "text/template"
  6. )
  7. func add(total, prod float64) float64 {
  8. return total + prod
  9. }
  10. func mul(quantity, price float64) float64 {
  11. return quantity * price
  12. }
  13. var funcMap = template.FuncMap{
  14. "add": add,
  15. "mul": mul,
  16. }
  17. func main() {
  18. t := template.Must(template.New("test").Funcs(funcMap).Parse(tmpl))
  19. var tpl bytes.Buffer
  20. b := TMPL{
  21. Total: 0,
  22. Shoes: []Shoe{
  23. Shoe{
  24. Quantity: 5,
  25. Price: 20,
  26. },
  27. },
  28. }
  29. err := t.Execute(&tpl, &b)
  30. if err != nil {
  31. fmt.Println(err)
  32. }
  33. fmt.Println(tpl.String())
  34. }
  35. type TMPL struct {
  36. Total int
  37. Shoes []Shoe
  38. }
  39. type Shoe struct {
  40. Quantity float64
  41. Price float64
  42. }
  43. var tmpl = `{{$Total := 0.00}}
  44. {{ range .Shoes }}
  45. {{ if .Quantity }}
  46. {{$Total = (add $Total (mul .Price .Quantity))}}
  47. {{ end }}
  48. {{ end }}
  49. {{printf "%.2f" $Total}}

playground

  1. 以上是你提供的代码的翻译结果。
  2. <details>
  3. <summary>英文:</summary>
  4. package main
  5. import (
  6. &quot;bytes&quot;
  7. &quot;fmt&quot;
  8. &quot;text/template&quot;
  9. )
  10. func add(total, prod float64) float64 {
  11. return total + prod
  12. }
  13. func mul(quantity, price float64) float64 {
  14. return quantity * price
  15. }
  16. var funcMap = template.FuncMap{
  17. &quot;add&quot;: add,
  18. &quot;mul&quot;: mul,
  19. }
  20. func main() {
  21. t := template.Must(template.New(&quot;test&quot;).Funcs(funcMap).Parse(tmpl))
  22. var tpl bytes.Buffer
  23. b := TMPL{
  24. Shoes: []Shoe{
  25. Shoe{
  26. 5,
  27. 20,
  28. },
  29. },
  30. }
  31. err := t.Execute(&amp;tpl, &amp;b)
  32. if err != nil {
  33. fmt.Println(err)
  34. }
  35. fmt.Println(tpl.String())
  36. }
  37. type TMPL struct {
  38. Total int
  39. Shoes []Shoe
  40. }
  41. type Shoe struct {
  42. Quantity float64
  43. Price float64
  44. }
  45. var tmpl = `{{$Total := 0.00}}
  46. {{ range .Shoes }}
  47. {{ if .Quantity }}
  48. {{ $Total = (add $Total (mul .Price .Quantity))}}
  49. {{ end }}
  50. {{ end }}
  51. {{printf &quot;%.2f&quot; $Total}}
  52. [playground][1]
  53. [1]: https://go.dev/play/p/RZ_qkM3pyBX
  54. </details>

huangapple
  • 本文由 发表于 2023年2月9日 11:31:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75393914.html
匿名

发表评论

匿名网友

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

确定