# 无法比较 x（类型为 struct { ... } 的变量）和 y（类型为 struct { ... } 的变量）

- **ID:** `go/struct-comparison-not-allowed`
- **领域:** go
- **类别:** type_error
- **验证级别:** ai_generated
- **修复率:** 88%

## 根因

包含不可比较字段（如切片、映射、函数）的结构体不能直接使用 == 或 != 进行比较。

## 版本兼容性

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

## 解决方案

1. ```
   Use reflect.DeepEqual() for deep comparison: if reflect.DeepEqual(a, b) { ... }
   ```
2. ```
   Implement a custom comparison function that manually compares each comparable field and uses reflect.DeepEqual for incomparable ones.
   ```
3. ```
   Redesign the struct to avoid incomparable fields (e.g., replace slices with arrays of fixed size).
   ```

## 无效尝试

- **** — Go does not support operator overloading; you cannot define a custom == method. (90% 失败率)
- **** — reflect.DeepEqual works but may have performance overhead and does not handle unexported fields in some cases. (40% 失败率)
- **** — Interface comparison still requires underlying values to be comparable; same error will occur. (70% 失败率)
