How to range over slice of structs instead of struct of slices

后端 未结 2 1421
一向
一向 2021-02-05 14:28

Having played around with Go HTML templates a bit, all the examples I found for looping over objects in templates were passing structs of slices to the template, somewhat like i

2条回答
  •  广开言路
    2021-02-05 15:14

    Use:

    {{range .}}
        {{.Id}}
        {{.Name}}
    {{end}}
    

    for the template.
    Here is a example: http://play.golang.org/p/A4BPJOcfpB
    You need to read more about the "dot" in the package overview to see how to properly use this. http://golang.org/pkg/text/template/#pkg-overview (checkout the Pipelines part)

提交回复
热议问题