为了应对Web应用程序的每个请求,有关模板正常代码是这样的:是否需要将模板放入地图中以便在Go中重用?
t:= template.New("welcome")
t, _ = t.ParseFiles("welcome.tpl")
t.Execute(w, data)
我猜ParseFiles
每次花费很多。是否有可能重用该模板?所以我改进这样的:
//templateMap := make(map[string][template])
//...
tplName :="welcome"
t := templateMap[tplName]
if t=nil{
t:= template.New(tplName)
t, _ = t.ParseFiles("welcome.tpl")
templateMap[tplName] = t
}
t.Execute(w, data)
我不知道它是可行或不可行通过将模板导入地图或缓存来提高效率? 我也想知道Execute
是线程安全还是不行?
func (t *Template) Execute(wr io.Writer, data interface{}) (err error)
有没有一种方便的方法使这种方法适用于使用继承的模板? (例如,哪里有一些模板的基础模板?) – carbocation 2013-04-10 14:54:38