# 无法将类型为 []T 的变量 x 用作类型 []U 进行追加

- **ID:** `go/mismatched-types-in-append`
- **领域:** go
- **类别:** type_error
- **验证级别:** ai_generated
- **修复率:** 82%

## 根因

Go 的类型系统强制执行严格的切片类型兼容性；即使底层类型相似，将一个具体类型的切片追加到另一个类型也需要显式转换或类型断言。

## 版本兼容性

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

## 解决方案

1. ```
   Convert each element individually in a loop: for _, v := range x { y = append(y, U(v)) }
   ```
2. ```
   If T and U are identical underlying types, use a simple copy: y = append(y, x...) after converting via unsafe or generics
   ```

## 无效尝试

- **Attempting to use a direct type assertion like x.([]U)** — Type assertion only works for interfaces, not for concrete slice types; it will cause a compile error. (95% 失败率)
- **Using reflect to convert slices at runtime** — Reflection is slow and error-prone for slice conversion; it doesn't solve the compile-time type mismatch. (85% 失败率)
- **Ignoring the error and forcing the append with unsafe.Pointer** — Using unsafe bypasses type safety, leading to potential memory corruption or undefined behavior. (99% 失败率)
