# 模板：:1:2: 执行 "" 在 <.Field>: 无法在类型 struct { ... } 中评估字段 Field

- **ID:** `go/template-cannot-evaluate-field-in-type-struct`
- **领域:** go
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 96%

## 根因

Go 模板引用了传递给 Execute() 的数据结构中不存在的字段或方法。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| go1.20 | active | — | — |
| go1.21 | active | — | — |
| go1.22 | active | — | — |

## 解决方案

1. ```
   在结构体中添加缺失的字段：type Data struct { Field string } 或将模板引用重命名为匹配现有字段
   ```
2. ```
   使用 template.FuncMap 定义自定义函数来获取值：tmpl.Funcs(template.FuncMap{"getField": func(d Data) string { return d.ExistingField }})
   ```
3. ```
   传递映射而不是结构体：data := map[string]interface{}{"Field": value}; tmpl.Execute(w, data)
   ```

## 无效尝试

- **Add the field to the template but not to the struct** — The error is from the missing field in the struct, not the template (80% 失败率)
- **Use {{.}} to print the whole struct and ignore the error** — Doesn't solve the underlying issue; the template may still fail for other fields (70% 失败率)
- **Change the struct to have a field with a different name** — The template expects the exact field name; renaming breaks the template match (60% 失败率)
