Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Templates golang输出模板_Templates_Go - Fatal编程技术网

Templates golang输出模板

Templates golang输出模板,templates,go,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) } 为什么不呢? test.html存在

如何显示模板的内容

包干管

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)

}
为什么不呢?
test.html存在

您不需要使用
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)
}
文字啦啦啦啦啦啦啦啦啦啦啦啦啦啦啦