go template_error ai_generated true

template: index.html:10:5: executing "index.html" at <.Foo>: can't evaluate field Foo in type main.Data

ID: go/template-execution-error

Also available as: JSON · Markdown
90%Fix Rate
90%Confidence
50Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1 active

Root Cause

Template execution failed because the data passed to the template does not match what the template expects. Common issues: unexported fields, missing fields, nil pointers in the template data, or calling methods with wrong arguments.

generic

Workarounds

  1. 92% success Ensure struct fields used in templates are exported (capitalized)
    type Data struct {
        Foo string  // exported, accessible in template as .Foo
    }

    Sources: https://pkg.go.dev/text/template#hdr-Actions

  2. 88% success Use template.FuncMap to register custom functions
    tmpl := template.New("").Funcs(template.FuncMap{
        "safeHTML": func(s string) template.HTML { return template.HTML(s) },
    })

    Sources: https://pkg.go.dev/text/template#FuncMap

Dead Ends

Common approaches that don't work:

  1. Use text/template instead of html/template to avoid escaping issues 85% fail

    text/template has the same evaluation rules — switching template packages does not fix data/field mismatches, and removes XSS protection

Error Chain

Leads to:
Preceded by:
Frequently confused with: