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

TemplatesGoBeego

Templates Problem Overview


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] }}

Templates Solutions


Solution 1 - Templates

You need to use the index template function.

{{index .a 2}}

Solution 2 - Templates

to access a field on an array item as go templating in *.yaml format :

{{ (index .Values.fields 0).fieldItem }}

Index of zero(0) as in fields array of field as fieldItem .

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGlstunnaView Question on Stackoverflow
Solution 1 - TemplatesKen BloomView Answer on Stackoverflow
Solution 2 - TemplatesSandeep JainView Answer on Stackoverflow