1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| var webpageTpl = ` [webpage] {{range $index, $n := .webpage}} {{ if isStart $index }} {{$index}}.{{$.Name}} {{.Desc}} {{end}} {{end}} `
data := []WebPage{ { Name: "w1", Next: []WebPage{ {Name: "w12"} } }, { Name: "w2", Next: []WebPage{ {Name: "w22"} } } }
tmpl, err := template.New("main").Funcs( template.FuncMap{ "isStart": func (i int) bool { return i == 0 } } ).Parse(webpageTpl) var buf bytes.Buffer tmpl.Execute(&buf, data) fmt.Println(buf.String())
|