{
  "id": "go/template-cannot-evaluate-field-in-type-struct",
  "signature": "template: :1:2: executing \"\" at <.Field>: can't evaluate field Field in type struct { ... }",
  "signature_zh": "模板：:1:2: 执行 \"\" 在 <.Field>: 无法在类型 struct { ... } 中评估字段 Field",
  "regex": "template:.*can't evaluate field (\\w+) in type (\\w+)",
  "domain": "go",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "A Go template references a field or method that does not exist on the data structure passed to Execute().",
  "root_cause_type": "generic",
  "root_cause_zh": "Go 模板引用了传递给 Execute() 的数据结构中不存在的字段或方法。",
  "versions": [
    {
      "version": "go1.20",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "go1.21",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "go1.22",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Add the field to the template but not to the struct",
      "why_fails": "The error is from the missing field in the struct, not the template",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    },
    {
      "action": "Use {{.}} to print the whole struct and ignore the error",
      "why_fails": "Doesn't solve the underlying issue; the template may still fail for other fields",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "Change the struct to have a field with a different name",
      "why_fails": "The template expects the exact field name; renaming breaks the template match",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Add the missing field to the struct: type Data struct { Field string } or rename the template reference to match an existing field",
      "success_rate": 0.96,
      "how": "Add the missing field to the struct: type Data struct { Field string } or rename the template reference to match an existing field",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use template.FuncMap to define a custom function that retrieves the value: tmpl.Funcs(template.FuncMap{\"getField\": func(d Data) string { return d.ExistingField }})",
      "success_rate": 0.93,
      "how": "Use template.FuncMap to define a custom function that retrieves the value: tmpl.Funcs(template.FuncMap{\"getField\": func(d Data) string { return d.ExistingField }})",
      "condition": "",
      "sources": []
    },
    {
      "action": "Pass a map instead of a struct: data := map[string]interface{}{\"Field\": value}; tmpl.Execute(w, data)",
      "success_rate": 0.9,
      "how": "Pass a map instead of a struct: data := map[string]interface{}{\"Field\": value}; tmpl.Execute(w, data)",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "在结构体中添加缺失的字段：type Data struct { Field string } 或将模板引用重命名为匹配现有字段",
    "使用 template.FuncMap 定义自定义函数来获取值：tmpl.Funcs(template.FuncMap{\"getField\": func(d Data) string { return d.ExistingField }})",
    "传递映射而不是结构体：data := map[string]interface{}{\"Field\": value}; tmpl.Execute(w, data)"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://pkg.go.dev/text/template#hdr-Actions",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.88,
  "fix_success_rate": 0.96,
  "resolvable": "true",
  "first_seen": "2023-06-12",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}