To deal with every request in a web application, the normal code about templates is like this:
t:= template.New("welcome")
t, _ = t.ParseFiles("welcome.tpl")
t.Execute(w, data)
I guess ParseFiles
every time cost a lot. Is it possible to reuse the template? So I improved it like this:
//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)
I wonder it is feasible or practicable to improve efficiency by putting templates into a map or a cache?
I also wonder the function Execute
is thread safe or not?
func (t *Template) Execute(wr io.Writer, data interface{}) (err error)