相关疑难解决方法(0)

去模板名称

html/template(和text/template)包中,template.New具有以下签名:

func New(name string) *Template
Run Code Online (Sandbox Code Playgroud)

究竟name用于什么?我已经扫描了文档(以及一些来源),但无济于事.我只是用空字符串实例化我的所有模板,它似乎没有什么区别.我为什么要打扰一个名字?

即使对于命名模板,这两者似乎是等价的:

template.Must(template.New("").Parse(`{{ define "body" }}Body{{ end }}`))
template.Must(template.New("body").Parse(`Body`))
Run Code Online (Sandbox Code Playgroud)

https://play.golang.org/p/wKzCHdLf2S

go go-templates

17
推荐指数
2
解决办法
3338
查看次数

使用"模板"包在golang中为客户端生成动态网页需要花费太多时间

使用template包为客户端生成动态网页时速度太慢.

测试代码如下,golang 1.4.1

http.Handle("/js/", (http.FileServer(http.Dir(webpath))))
http.Handle("/css/", (http.FileServer(http.Dir(webpath))))
http.Handle("/img/", (http.FileServer(http.Dir(webpath))))
http.HandleFunc("/test", TestHandler)


func TestHandler(w http.ResponseWriter, r *http.Request) {

    Log.Info("Entering TestHandler ...")
    r.ParseForm()

    filename := NiConfig.webpath + "/test.html"

    t, err := template.ParseFiles(filename)
    if err != nil {
        Log.Error("template.ParseFiles err = %v", err)
    } 

    t.Execute(w, nil)
}
Run Code Online (Sandbox Code Playgroud)

根据日志,我发现它花了大约3秒钟t.Execute(w, nil),我不知道它为什么用这么多时间.我也试过Apache服务器进行测试test.html,它响应速度非常快.

templates http go go-templates server

5
推荐指数
1
解决办法
1132
查看次数

标签 统计

go ×2

go-templates ×2

http ×1

server ×1

templates ×1