Get index number from range
Asked Answered
S

1

6

In to get the index number php:

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

How can I get the index number in go?

{{ range .post }}
   {{ // how can i get index number? }}
   {{ .Id }}
   {{ .Name}}
{{ end}}
Swivet answered 29/8, 2018 at 10:24 Comment(0)
C
10

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

So in your example:

{{ range $i, $e := .post }}
   Index: {{ $i }}
   {{ .Id }}
   {{ .Name}}
{{ end}}
Cholecystotomy answered 29/8, 2018 at 10:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.