关于 text/template 包的 Go 文档非常抽象,以至于我无法弄清楚如何实际覆盖对象切片。到目前为止,这是我的尝试(这对我没有任何输出):package mainimport ( "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)}
- 1 回答
- 0 关注
- 182 浏览
添加回答
举报
0/150
提交
取消