# rpc错误：代码=超出范围 描述=索引10超出列表长度5的范围

- **ID:** `go/grpc-out-of-range-index-error`
- **领域:** go
- **类别:** data_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

客户端请求的索引超出了响应中列表或数组的大小。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 1.0 | active | — | — |
| 1.1 | active | — | — |

## 解决方案

1. **Validate the index before making the request.** (95% 成功率)
   ```
   if idx < 0 || idx >= len(items) {
    return fmt.Errorf("index out of range")
}
resp, err := client.GetItem(ctx, &pb.GetRequest{Index: int32(idx)})
   ```
2. **Fetch the entire list and then access the index locally.** (90% 成功率)
   ```
   listResp, err := client.GetList(ctx, &pb.ListRequest{})
if err != nil { return err }
if idx < 0 || idx >= len(listResp.Items) {
    return fmt.Errorf("index out of range")
}
item := listResp.Items[idx]
   ```

## 无效尝试

- **Assume the server will handle out-of-range gracefully.** — The server returns an error; it does not auto-correct. (100% 失败率)
- **Use negative indexing hoping it wraps around.** — Protobuf does not support negative indexing; it will still fail. (90% 失败率)
