There is no reason to check for nil in the code provided in the article. There are other ways to structure the code.
Option 1:
var templates = map[string]*template.Template{}
func init() {
// code following the if statement from the function in the article
}
Option 2:
var templates = initTemplates()
func initTemplates() map[string]*template.Template{} {
templates := map[string]*template.Template{}
// code following the if statement from the function in the article
return templates
}
Option 3:
func init() {
templates = make(map[string]*template.Template)
// code following the if statement from the function in the article
}
You will see all of these approaches in Go code. I prefer the second option because it makes it clear that templates
is initialized in the function initTemplates
. The other options require some looking around to find out where templates
is initialized.