liveBook · Manning

ParseFS

As explained in Learn Go with Pocket-Sized Projects

ParseFS is a Go template API method that parses templates from files obtained through an io/fs file system.

ParseFS parses template files obtained through an io/fs file system and registers them by base filename. With //go:embed, the files are available in memory instead of being opened on every request. section 11.5.3

Parse embedded templates

Embed only the templates needed by the page, then parse them with a template name and function map: template.New("index").Funcs(funcs).ParseFS(templates, "templates/*.html"). When multiple files are loaded, execute the intended file explicitly with ExecuteTemplate(w, "index.html", data). section 11.5.3

Listing 11.46 index.html: Embedding the contents of a directory
//go:embed templates/*.html #1
var templates embed.FS

...
tpl, err := template.New("index").Funcs(template.FuncMap{
    "statusCSSClass": statusCSSClass,
    "progress":       progress,
}).ParseFS(templates, "templates/*.html") #2

Shows the complete //go:embed declaration and ParseFS call for a directory of HTML templates. section 11.5.3

How Learn Go with Pocket-Sized Projects covers it

How deep it goes
Explains the call shape, filename registration, ambiguity error, and required ExecuteTemplate choice without detailing parser internals.
Examples
Uses embedded HTML files matched by templates/*.html and demonstrates the incomplete-template error caused by ambiguous execution.
What it assumes
Assumes Go template execution, template definitions, and //go:embed usage.
What you can do after
Operate/configure Go template loading from embedded files and select the correct parsed template during execution.
How it connects
Connects ParseFS with io/fs, embed.FS, template.New, FuncMap, and ExecuteTemplate.

This explainer was generated by Manning's knowledge graph from the text of Learn Go with Pocket-Sized Projects. The section links point to the parts of the book it draws on.