Golang separating items with comma in template

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

Golang separating items with comma in template

问题

我正在尝试显示一个逗号分隔的值列表,并且不想在最后一项(或者如果只有一项的话,是唯一的项)后面显示逗号。

到目前为止,我的代码是这样的:

设备:
    {{$equipment := .Equipment}}
    {{ range $index, $element := .Equipment}}
        {{$element.Name}}
        {{if lt $index ((len $equipment) -1)}}
            ,
        {{end}}
    {{end}}

当前的输出是:设备:Mat , Dumbbell ,
如何去掉末尾的逗号?

英文:

I am trying to display a list of comma separated values, and don't want to display a comma after the last item (or the only item if there is only one).

My code so far:

Equipment:
	{{$equipment := .Equipment}}
	{{ range $index, $element := .Equipment}}
		{{$element.Name}}
		{{if lt $index ((len $equipment) -1)}}
			,
		{{end}}
	{{end}}

The current output: Equipment: Mat , Dumbbell ,
How do I get rid of the trailing comma

答案1

得分: 122

一个很好的技巧是:

设备:
    {{$equipment := .Equipment}}
    {{ range $index, $element := .Equipment}}
        {{if $index}},{{end}}
        {{$element.Name}}
    {{end}}

这个技巧的原理是,第一个索引是0,在if语句中返回false。所以这段代码对于第一个索引返回false,然后在每次迭代之前放置一个逗号。这样就得到了一个没有前导或尾随逗号的逗号分隔列表。

英文:

A nice trick you can use is:

Equipment:
    {{$equipment := .Equipment}}
    {{ range $index, $element := .Equipment}}
        {{if $index}},{{end}}
        {{$element.Name}}
    {{end}}

This works because the first index is 0, which returns false in the if statement. So this code returns false for the first index, and then places a comma in front of each following iteration. This results in a comma separated list without a leading or trailing comma.

答案2

得分: 42

为了帮助你完成这项工作,你可以添加一个模板函数来实现。strings.Join非常适合你的用例。

假设tmpl包含你的模板,你可以将Join函数添加到你的模板中:

tmpl = tmpl.Funcs(template.FuncMap{"StringsJoin": strings.Join})

模板:

Equipment:
    {{ StringsJoin .Equipment ", " }}

Playground

文档:https://golang.org/pkg/text/template/#FuncMap

英文:

Add a template function to do the work for you. strings.Join is perfect for your use case.

Assuming tmpl contains your templates, add the Join function to your template:

tmpl = tmpl.Funcs(template.FuncMap{"StringsJoin": strings.Join})

Template:

Equipment:
    {{ StringsJoin .Equipment ", " }}

Playground

Docs: https://golang.org/pkg/text/template/#FuncMap

答案3

得分: 2

不带索引

它可以用于任何迭代,例如在没有索引的地图上:

    {{ $first := true }}
    {{ range $_, $element := .}}
        {{if not $first}}, {{else}} {{$first = false}} {{end}}
        {{$element.Name}}
    {{end}}

Playground

英文:

without index

it can be useful for any iteration, like on maps that do not have index:

    {{ $first := true }}
    {{ range $_, $element := .}}
        {{if not $first}}, {{else}} {{$first = false}} {{end}}
        {{$element.Name}}
    {{end}}

Playground

答案4

得分: 1

另一种方法是为字段创建一个新类型。

type MyList []string

func (list MyList) Join() string {
    return strings.Join(list, ", ")
}

然后你可以像普通方式一样在模板中使用该函数:

{{ .MyList.Join }}
英文:

Another way to skin the cat, if you can create a new type for the field.

type MyList []string

func (list MyList) Join() string {
    return strings.Join(list, ", ")
}

Then you can use the function in the template like regular:

{{ .MyList.Join }}

答案5

得分: -2

如果你愿意使用外部库,似乎sprig库有一个"join"函数(参见这里):

join

将字符串列表连接成一个字符串,使用给定的分隔符。

list "hello" "world" | join "_"

上述代码将生成hello_world

join函数会尝试将非字符串转换为字符串值:

list 1 2 3 | join "+"

上述代码将生成1+2+3

英文:

If you are willing to use an external library, it seems like sprig library has a "join" function (see here):

> # join
>
> Join a list of strings into a single string, with the given separator.
>
> list "hello" "world" | join "_"
>
> The above will produce hello_world
>
> join will try to convert non-strings to a string value:
>
> list 1 2 3 | join "+"
>
> The above will produce 1+2+3

huangapple
  • 本文由 发表于 2014年1月23日 18:45:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/21305865.html
匿名

发表评论

匿名网友

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

确定