模板包--text/template
模板包--text/template
package main
import (
"bytes"
"fmt"
"github.com/xbitgo/core/tools/tool_file"
"text/template"
)
func main() {
type users struct {
NameList []string
}
u := []string{"jeff", "汤姆", "杰克逊"}
userList := users{u}
temp := `{{range $name := .NameList}}我的名字是:{{$name}}
{{end}}
`
tmpl, err := template.New("test").Parse(temp)
if err != nil {
return
}
buf := new(bytes.Buffer)
if err := tmpl.Execute(buf, userList); err != nil {
fmt.Println("err:", err.Error())
return
}
fmt.Println(buf)
//写入文件
err = tool_file.WriteFile("./golang/test.go", buf.Bytes())
if err != nil {
fmt.Println("err:", err.Error())
return
}
}
选择了IT,必定终身学习