3

I want to pass a slice that contains structs and display all of them in the view.

type Person struct {
    ID   int
    NAME string
}

Example of a slice of structs

 [{1 John},{2, Mary},{3, Steven},{4, Mike}]

What I want in index.html

1 - John
2 - Mary
3 - Steven
4 - Mike
snowpeak
  • 797
  • 9
  • 25
  • Use the range action. Go to https://golang.org/pkg/text/template/ and ctrl+f for `range`. – mkopriva Aug 17 '18 at 20:37
  • I found a relevant post and I solved it. Thank you guys. https://stackoverflow.com/questions/24556001/go-templates-range-over-slice-of-structs-instead-of-struct-of-slices – snowpeak Aug 17 '18 at 20:54

1 Answers1

1

I found a relevant post and I solved it. Thank you guys.

Go templates : range over slice of structs instead of struct of slices

So as mkopriva mentioned, use range action.

snowpeak
  • 797
  • 9
  • 25