Go templates: How do I access array item (arr[2]) in templates?

Glstunna picture Glstunna · Jul 5, 2015 · Viewed 27.5k times · Source

How do I access an array item (e.g. a[2]) in templates?

Whenever I do this I get "bad character U+005B '['"

{{ .a[2] }}

Answer

Ken Bloom picture Ken Bloom · Jul 5, 2015

You need to use the index template function.

{{index .a 2}}