Created on August 7, 2025
Goodbye, markdown template cache
// main.go
type application struct {
// ...
markdownHTMLCache map[string]template.HTML
// ...
}
func main() {
// ...
markdownHTMLCache, err := newMarkdownHTMLCache()
if err != nil {
log.Fatalf("could not initialise markdownHTMLCache: %v\n", err)
}
app.markdownHTMLCache = markdownHTMLCache
// ...
}
// helpers.go
func newMarkdownHTMLCache() (map[string]template.HTML, error) {
pages := make(map[string]template.HTML)
files := []string{
"home.md",
"now.md",
"about.md",
"blog.md",
"bookshelf.md",
}
for _, file := range files {
md, err := os.ReadFile("./data/pages/" + file)
if err != nil {
return nil, fmt.Errorf("could not read file: %v", err)
}
htmlBytes := blackfriday.Run(md)
content := template.HTML(htmlBytes)
pages[file] = content
}
return pages, nil
}
// handlers.go
func (app *application) home(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Started-Working-On", "April-2024")
t := app.newTemplateData(r)
t.HTML = app.markdownHTMLCache["home.md"]
app.render(w, r, 200, "home.tmpl.html", &t)
}