具体范围示例

The Go documentation on the text/template package is so abstract that I'm having trouble figuring out how to actually range over a slice of objects. Here's my attempt so far (This outputs nothing for me):

package main
import (
    "os"
    templ "text/template"
)
type Context struct {
    people []Person
}
type Person struct {
    Name   string //exported field since it begins with a capital letter
    Senior bool
}
func main() {
    // Range example 
    tRange := templ.New("Range Example")
    ctx2 := Context{people: []Person{Person{Name: "Mary", Senior: false}, Person{Name: "Joseph", Senior: true}}}
    tRange = templ.Must(
    tRange.Parse(`
{{range $i, $x := $.people}} Name={{$x.Name}} Senior={{$x.Senior}}  {{end}}
`))
    tRange.Execute(os.Stdout, ctx2)
}

The range is correct. The problem is that the Context people field is not exported. The template package ignores unexported fields. Change the type definition to:

type Context struct {
   People []Person // <-- note that People starts with capital P.
}

and the template to:

 {{range $i, $x := $.People}} Name={{$x.Name}} Senior={{$x.Senior}}  {{end}}

playground