Virgil Nauleau
Virgil Nauleau

Reputation: 139

Using HTML Template with golang, how do I print index of the given data in the HTML?

i'm doing a school work, i need to do an Html Template using go, i give it a data fetched by the go, i want in the html if possible to print the first index (for testing it could be other index later) of the data (the data is All called by . in html .Artists has a list of group and i only want to print the first one but {{.Artists[0]}} is crashing my html) i do not find other way on internet expept 'range' but i cant get the index value i want i just get the index of each name printed :

{{range $i, $Artists := .Artists}}
            {{$i $Artists}}
        {{end}} 

what ive tryed from the net. I tryed $Artists$0 and other thing without succes, i know i could go with a go form but i want to try it in the html. Thanks in advance for the help :)

Edit:(the go has no probleme doing .Artist print all the group but i just want to print one particular one for now)

Upvotes: 1

Views: 517

Answers (1)

Virgil Nauleau
Virgil Nauleau

Reputation: 139

You want to print the first element of $Artists? Use the index function: {{index $Artists 0}}

Thanks to icza , that's what i was searching !

Upvotes: -1

Related Questions