Skip to content

Instantly share code, notes, and snippets.

@emctague
Created April 16, 2022 03:47
Show Gist options
  • Save emctague/86ec89492ac40318b9b5999d18917b9f to your computer and use it in GitHub Desktop.
Save emctague/86ec89492ac40318b9b5999d18917b9f to your computer and use it in GitHub Desktop.
Golang Template Abstraction for Debugging
type TplRunner interface {
Execute(w io.Writer, data any) error
}
type DebugTplRunner string
func (r DebugTplRunner) Execute(w io.Writer, data any) error {
t, err := template.ParseFiles(string(r))
if err != nil {
return err
}
return t.Execute(w, data)
}
// Now, both template.Template and DebugTplRunner can be used as type TplRunner.
// This allows for switching on/off automatic template reloading, e.g.:
//
// var tpl TplRunner
// if debug {
// tpl = DebugTplRunner("index.html")
// } else {
// tpl = template.Must(template.ParseFiles("index.html"))
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment