Special case treatment for the last element of a range in Google Go's text templates

前端 未结 1 1359
猫巷女王i
猫巷女王i 2021-01-12 03:11

I\'m trying to write a string looking like this using go\'s template system: (p1, p2, p3), where p1, p2, ... comes from an array in the program. My problem is how to place t

1条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-12 03:49

    See this example from the go-nuts mailing list. One key to this trick is that a template if is different than a Go language if. A template can test for a value of zero, unlike the Go language that requires a boolean. The magic is then {{if $index}},{{end}} where $index needs no declaration other than its appearance in the assignment.

    0 讨论(0)
提交回复
热议问题