html - 从范围中获取索引号

标签 html loops templates go go-templates

在获取索引号的php中:

foreach much as index => each{
 //get index number from index
}

go中如何获取索引号?

{{ range .post }}
   {{ // how can i get index number? }}
   {{ .Id }}
   {{ .Name}}
{{ end}}

最佳答案

text/template Variables:

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:

range $index, $element := pipeline

所以在你的例子中:

{{ range $i, $e := .post }}
   Index: {{ $i }}
   {{ .Id }}
   {{ .Name}}
{{ end}}

关于html - 从范围中获取索引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52075151/

相关文章:

linux - Bash for 循环可变数字

javascript - 带有列表组的 Bootstrap 4 scrollspy 不起作用

html - CSS 菜单在打开时覆盖其他内容

php - htaccess - 将 .shtml 和 .html 重写为 . PHP

ruby - 以下哪项是最有效的

php - 循环并显示 php 列中的所有数据库条目

python - Eclipse 模板中的条件语句

c++ - 函数特化

c++ - 当参数是函数参数包时,在部分排序期间推导模板参数

javascript - 部分文本框只读,具有默认值