golang输出模板

Acc*_*cex 5 templates go

如何显示模板的内容?

包主

import (
    "fmt"
    "html/template"
    "os"

)

func main() {
    t := template.New("another")
    t,e:=t.ParseFiles("test.html")
    if(e!=nil){
            fmt.Println(e);
    }
    t.Execute(os.Stdout, nil)

}
Run Code Online (Sandbox Code Playgroud)

为什么不呢?test.html存在

小智 7

您不需要创建新模板,New然后ParseFiles在其上使用.还有一个功能ParseFiles,负责在幕后创建一个新模板.
这是一个例子:

package main

import (
    "fmt"
    "html/template"
    "os"
)

func main() {
    t, err := template.ParseFiles("test.html")
    if err != nil {
            fmt.Println(err);
    }
    t.Execute(os.Stdout, nil)
}
Run Code Online (Sandbox Code Playgroud)