1
mind3x 2016-08-31 18:59:48 +08:00 1
https://golang.org/pkg/text/template/
一开始就有 example 啊... Here is a trivial example that prints "17 items are made of wool". ``` type Inventory struct { Material string Count uint } sweaters := Inventory{"wool", 17} tmpl, err := template.New("test").Parse("{{.Count}} items are made of {{.Material}}") if err != nil { panic(err) } err = tmpl.Execute(os.Stdout, sweaters) if err != nil { panic(err) } ``` |
3
hyq 2016-08-31 19:29:30 +08:00 1
用 bytes.Buffer 就行
|
6
hyq 2016-09-01 01:55:17 +08:00 1
package main
import "text/template" import "fmt" import "bytes" type Class struct{ CountPersion uint32 CountPassed uint32 TeacherName string } func main() { str := "全班一共{{ .CountPersion }}人,其中有{{ .CountPassed }}人及格,班主任是{{ .TeacherName }}" tpl := template.Must(template.New("test").Parse(str)) b := &bytes.Buffer{} tpl.Execute(b, &Class{1,2,"Lili"}) fmt.Println(b.String()) } |