For example
{{ range .cars }}
{{.color}} , ${{.price}}
{{ end }}
The array size of 'cars' is 10. How do I get the index value?
When the index is 5, I want do some process.
I am using Beego web framework.
For example
{{ range .cars }}
{{.color}} , ${{.price}}
{{ end }}
The array size of 'cars' is 10. How do I get the index value?
When the index is 5, I want do some process.
I am using Beego web framework.
First of all, the answer to your question was given in the comment by topskip.
range $index, $element := pipeline
See the documentation at golang.org/pkg/text/template/#hdr-Variables.
Secondly, bear in mind that templates rapidly get too complex when you start putting logic in, such as treating the 5th element differently.
With this in mind, it is a good idea instead to try to design the view models so that they are simple to render. More logic might be needed in your controllers, but it's usually easier to test there. Alternatively, consider using helper utilities written in Go.
Less logic in the templates will make things generally easier.