gpt4 book ai didi

templates - golang 输出模板

转载 作者:IT王子 更新时间:2023-10-29 01:26:15 24 4
gpt4 key购买 nike

如何显示模板内容?

主要包

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)
}

关于templates - golang 输出模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10185942/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com