【问题标题】:Get index number from range从范围中获取索引号
【发布时间】:2019-02-04 02:34:38
【问题描述】:

在php中获取索引号:

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

如何获取go中的索引号?

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

【问题讨论】:

    标签: html loops templates go go-templates


    【解决方案1】:

    text/template Variables:

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

    range $index, $element := pipeline
    

    所以在你的例子中:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-09
      • 1970-01-01
      • 1970-01-01
      • 2021-06-01
      • 2017-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多