M
M
Michail Wowtschuk2018-09-18 13:10:43
go
Michail Wowtschuk, 2018-09-18 13:10:43

Is template parsing happening at compile time or at runtime of the GO code?

Please do not kick much, I just like to know how it works inside and how to optimize and speed it up.
Explain how template parsing works internally.
Which option is better in terms of performance and resources.
Option 1 - Global variable

var t, err = template.ParseFiles("templates/wt/index.html", "templates/wt/header.html", "templates/wt/footer.html")
  
func main(){

}

func indexHandler(w, r){
  t.ExecuteTemplate(w, "index", locale)
}

Option 2 - In the main function
func main(){
  t, err := template.ParseFiles("templates/wt/index.html", "templates/wt/header.html", "templates/wt/footer.html")
}

func indexHandler(w, r, t){
  t.ExecuteTemplate(w, "index", locale)
}

Option 3 - In a processed function
func main(){
  
}

func indexHandler(w, r){
  t, err := template.ParseFiles("templates/wt/index.html", "templates/wt/header.html", "templates/wt/footer.html")
  t.ExecuteTemplate(w, "index", locale)
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Burov, 2018-09-18
@wowtschuk

In the first option, how will you handle errors?
In the third option, for each request, files with templates will be opened and parsed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question