//把数据写到文件里面
package main import (
"fmt"
"text/template"
"time"
"os"
) type test struct {
Time string
Res string
} func main () {
//创建文件
file, err := os.Create("test.log")
if err != nil{
fmt.Println("file create failed, error:", err)
return
} defer file.Close() t, err := template.ParseFiles("./demo02.log") if err != nil {
fmt.Println("template parse failed, error:", err)
return
} t1 := test{
time.Now().Format("2006-01-02 15:04:05"),
"success",
} err = t.Execute(file, t1)
if err != nil {
fmt.Println("template execute failed, error:", err)
return
} }
demo2.log
时间:{{.Time}}, 执行{{.Res}}
结果