1.渲染终端

最后更新于:2022-04-02 04:46:32

模板渲染终端 模板替换 {{.字段名}} ~~~ main.go package main import ( "fmt" "os" "text/template" ) type Person struct { Name string age string } func main() { t, err := template.ParseFiles("./index.html") if err != nil { fmt.Println("parse file err:", err) return } p := Person{Name: "Mary", age: "31"} if err := t.Execute(os.Stdout, p); err != nil { fmt.Println("There was an error:", err.Error()) } } ~~~ index.html ~~~

hello,{{.Name}} {{.}}

~~~ 然后在终端就可以渲染输出如下: $ go run main.go ~~~

hello,Mary {Mary 31}

~~~
';