Go 模板中的结构方法通常与公共结构属性的调用方式相同,但在这种情况下它不起作用:http ://play.golang.org/p/xV86xwJnjA
{{with index . 0}}
{{.FirstName}} {{.LastName}} is {{.SquareAge}} years old.
{{end}}
错误:
executing "person" at <.SquareAge>: SquareAge is not a field
of struct type main.Person
同样的问题:
{{$person := index . 0}}
{{$person.FirstName}} {{$person.LastName}} is
{{$person.SquareAge}} years old.
相反,这有效:
{{range .}}
{{.FirstName}} {{.LastName}} is {{.SquareAge}} years old.
{{end}}
如何在 {{with}} 和 {{$person}} 示例中调用 SquareAge() 方法?