Render an HTML template in Golang

Constantin Druc ยท 24 Jul, 2024

Reasons for template.ParseFiles to return an error:

  • the file doesn't exist
  • missing read permissions
  • there's a syntax error in the template

Reasons for t.Execute to return an error:

  • missing field on struct
  • calling a function that does not exist

What file extension to use?: You can use any extension you want, golang doesn't care. I usually stick with good old .html

Why is the template.ParseFiles plural?: You can parse more than one HTML file into a template. Usually you do so to create a template from many bits and pieces (layouts and partials in special).