在Google Go的文本模板中,对于范围的最后一个元素进行特殊处理。

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

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

问题

我正在尝试使用Go的模板系统编写一个类似于这样的字符串:(p1, p2, p3),其中p1、p2等来自程序中的一个数组。我的问题是如何正确地放置逗号,以便最后一个(或第一个)元素。

我的非工作版本输出(p1, p2, p3, ),如下所示:

package main

import "text/template"
import "os"
func main() { 
    ip := []string{"p1", "p2", "p3"}
    temp := template.New("myTemplate")
    temp,_ = temp.Parse(paramList)
    temp.Execute(os.Stdout, ip)

}

const paramList = 
`{{ $i := . }}({{ range $i }}{{ . }}, {{end}})`

到目前为止,我找到的最好的线索在这里找到:http://golang.org/pkg/text/template/,在下面的语句中:

如果“range”操作初始化一个变量,该变量将设置为迭代的连续元素。此外,“range”可以声明两个变量,用逗号分隔:

$index, $element := pipeline

在这种情况下,$index和$element分别设置为数组/切片索引或映射键和元素的连续值。请注意,如果只有一个变量,它将被赋予元素;这与Go范围子句中的约定相反。
其中建议使用索引

这表明可以在迭代中获取索引,但我只是无法弄清楚range声明两个变量的含义,以及在模板中应该声明这些变量的位置。

英文:

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 the comma properly for the last (or the first) element.

My non working version that outputs (p1, p2, p3, ) looks like this:

package main

import "text/template"
import "os"
func main() { 
	ip := []string{"p1", "p2", "p3"}
 	temp := template.New("myTemplate")
	temp,_ = temp.Parse(paramList)
 	temp.Execute(os.Stdout, ip)

}

const paramList = 
`{{ $i := . }}({{ range $i }}{{ . }}, {{end}})`

My best clue so far is found here http://golang.org/pkg/text/template/ in the following statement:

If a "range" action initializes a variable, the variable is set to the successive elements of the iteration. Also, a "range" may declare two variables, separated by a comma:

$index, $element := pipeline

in which case $index and $element are set to the successive values of the array/slice index or map key and element, respectively. Note that if there is only one variable, it is assigned the element; this is opposite to the convention in Go range clauses.
where it's suggested that the index

This suggests that it's possible to get hold of the index in the iteration but I just can't figure out what is meant with the range declaring two variables and where in the template those variables are supposed to be declared.

答案1

得分: 15

请参考这个例子来自go-nuts邮件列表。这个技巧的关键之一是模板中的if与Go语言中的if是不同的。模板可以测试零值,而Go语言则需要一个布尔值。魔法在于{{if $index}},{{end}},其中$index不需要声明,只需要在赋值中出现即可。

英文:

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.

huangapple
  • 本文由 发表于 2012年5月25日 08:30:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/10747054.html
匿名

发表评论

匿名网友

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

确定