我是新来的。我一直在搜索文档。在下面的游乐场代码中,它正在屏幕上渲染和打印它。我希望将呈现的文本存储在字符串中,以便我可以从函数中返回它。
package main
import (
"os"
"text/template"
)
type Person struct {
Name string //exported field since it begins with a capital letter
}
func main() {
t := template.New("sammple") //create a new template with some name
t, _ = t.Parse("hello {{.Name}}!") //parse some content and generate a template, which is an internal representation
p := Person{Name:"Mary"} //define an instance with required field
t.Execute(os.Stdout, p) //merge template ‘t’ with content of ‘p’
}
https://play.golang.org/p/-qIGNSfJwEX
怎么做 ?
Helenr
相关分类