Go alternative for python loop.last

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

Go alternative for python loop.last

问题

我正在寻找一种在Go模板中循环遍历数组的方法,并且我想在循环中的最后一个项目中添加额外的字符串。

在Python中,我可以这样做:

{% for host in hosts %}
{{ host }}{% if loop.last %} ;{% endif %}
{% endfor %}

希望在Go中实现相同的效果,以下是Go等效代码片段:

{{ range $host := $hosts }}
{{$host}}
{{ end }}

谢谢。

英文:

I'm looking to loop over an array with Go templates and I want to add an extra string to the last item in the loop.

In python, I can do

{% for host in hosts %}
{{ host }}{% if loop.last %} ;{% endif %}
{% endfor %}

Looking to achieve same thing with Go, below is the snippet of the Go equivalent.

{{ range $host := $hosts }}
{{$host}}
{{ end }}

Thanks.

答案1

得分: 3

如果列表不为空,则Python代码片段会在最后一项后面打印一个分号。在Go语言中,你可以通过在range循环外部使用if语句来检查切片中是否至少有一个元素,并在循环外部打印分号。

{{if $hosts}}{{range $host := $hosts}}
{{$host}}
{{ end }} ;{{end}}

这个代码片段之所以有效,是因为它是在最后一项的末尾添加的。更通用的解决方案需要使用自定义模板函数。下面是一个示例函数:

func last(v interface{}, i int) (bool, error) {
  rv := reflect.ValueOf(v)
  if rv.Kind() != reflect.Slice {
    return false, errors.New("not a slice")
  }
  return rv.Len()-1 == i, nil
}

以下是在模板中如何使用它的示例:

{{range $i, $host := $hosts }}
{{$host}}{{if last $hosts $i}} ;{{end}}
{{ end }}

我在playground上发布了一个自定义函数的工作示例

英文:

If the list is not empty, then the Python snippet prints a semicolon following the last item. You can achieve the same result in Go by surrounding the range with an if to check to see if there's at least one element in the slice and printing the ; outside of the loop.

{{if $hosts}}{{range $host := $hosts}}
{{$host}}
{{ end }} ;{{end}}

This snippet works because you are adding to the end of the last item. A more general solution requires a custom template function. Here's an example function:

func last(v interface{}, i int) (bool, error) {
  rv := reflect.ValueOf(v)
  if rv.Kind() != reflect.Slice {
	return false, errors.New("not a slice")
  }
  return rv.Len()-1 == i, nil
}

and here's how to use it in the template:

{{range $i, $host := $hosts }}
{{$host}}{{if last $hosts $i}} ;{{end}}
{{ end }}

I posted an a working example of the custom function to the playground.

答案2

得分: 0

一种替代方法是定义一个递减函数,例如:

	"dec": func(n int) int { return n - 1 },

然后可以使用dec函数来计算最后一个元素,例如:

{{$last := dec (len $hosts)}}
{{range $i, $host := $hosts}}
{{$host}}{{if eq $i $last}} ;{{end}}
{{end}}

当然,如果Go模板允许减法运算,那么写成{{$last := (len $hosts) - 1}}会更容易。毕竟,他们坚持在空格减号之前或之后有一个空格,那为什么不允许简单的算术运算呢?

英文:

An alternative is to define a decrement function, e.g.,

	"dec": func(n int) int { return n - 1 },

You can then use the dec function to calculate the last element, e.g.,

{{$last := dec (len $hosts)}}
{{range $i, $host := $hosts}}
{{$host}}{{if eq $i $last}} ;{{end}}
{{end}}

Of course, it would be easier if Go templates allowed subtraction so you could write {{$last := (len $hosts) - 1}}. After all, they insist on having a space before or after a spacing minus sign, so why not allow simple arithmetic?

huangapple
  • 本文由 发表于 2014年11月30日 05:08:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/27207131.html
匿名

发表评论

匿名网友

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

确定