Use template.ParseFiles to load multiple template files, then call ExecuteTemplate on the parent template to render a specific partial by name. This allows you to define reusable components in separate files and inject them into a main layout.
package main
import (
"html/template"
"os"
)
func main() {
// Load the main template and the partial
tmpl, err := template.ParseFiles("layout.html", "header.html", "footer.html")
if err != nil {
panic(err)
}
// Execute the main template, which includes the partials
err = tmpl.ExecuteTemplate(os.Stdout, "layout.html", nil)
if err != nil {
panic(err)
}
}
In your layout.html, reference the partials using {{template "header.html"}} or {{template "footer.html"}}.