简体   繁体   中英

How to pass a slice of structs to template and iterate them in Golang?

I want to pass a slice that contains structs and display the all the values 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM