Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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] }} 
like image 533
Glstunna Avatar asked Jul 05 '15 21:07

Glstunna


People also ask

What is dot in Go template?

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.

What is Gomplate?

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 !

What does mean in Go template?

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 .


1 Answers

You need to use the index template function.

{{index .a 2}} 
like image 56
Ken Bloom Avatar answered Sep 29 '22 21:09

Ken Bloom