# 恐慌：json：无限递归（无效循环）

- **ID:** `go/encoding-json-marshal-infinite-recursion`
- **领域:** go
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

结构体存在循环引用，导致json.Marshal无限递归，最终栈溢出。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 1.20 | active | — | — |
| 1.21 | active | — | — |

## 解决方案

1. **Implement json.Marshaler to break cycles or use pointer indirection** (90% 成功率)
   ```
   type Node struct {
    Value int
    Next  *Node
}
func (n *Node) MarshalJSON() ([]byte, error) {
    if n == nil { return json.Marshal(nil) }
    return json.Marshal(struct{ Value int }{Value: n.Value})
}
   ```

## 无效尝试

- **Adding a depth counter to break recursion** — May still panic if depth exceeded; better to use custom marshaler. (50% 失败率)
