# 恐慌：运行时错误：切片索引越界

- **ID:** `go/unsafe-string-conversion`
- **领域:** go
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

使用`unsafe`将`[]byte`转换为`string`时未确保正确长度，导致越界读取。

## 版本兼容性

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

## 解决方案

1. **Use `string(b)` for safe conversion (copies)** (100% 成功率)
   ```
   s := string(b)
// s is immutable copy
   ```
2. **Use `unsafe.String` with explicit length (Go 1.20+)** (80% 成功率)
   ```
   s := unsafe.String(&b[0], len(b))
// but ensure b is not mutated
   ```

## 无效尝试

- **Using `*(*string)(unsafe.Pointer(&b))` directly** — String header may have wrong length if slice header changes. (90% 失败率)
- **Assuming zero-copy is always safe** — If slice is modified later, string becomes invalid. (70% 失败率)
