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] }}
Anything within {{ }} inside the template string is where we do something with the data that we pass in when executing the template. This something can be just displaying the data, or performing certain operations with it. The . (dot) refers to the data that is passed in.
gomplate is a template renderer which supports a growing list of datasources, such as: JSON (including EJSON - encrypted JSON), YAML, AWS EC2 metadata, and secrets. Come chat with developers and community in the on and on !
Go templates are a powerful method to customize output however you want, whether you're creating a web page, sending an e-mail, working with Buffalo, Go-Hugo, or just using some CLI such as kubectl. There're two packages operating with templates — text/template and html/template .
You need to use the index
template function.
{{index .a 2}}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With