# proto：oneof 字段 "pb.Event.payload" 已使用不同类型设置

- **ID:** `go/proto-oneof-set-conflict`
- **领域:** go
- **类别:** data_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

代码尝试在同一消息上设置两个不同的 oneof 变体。oneof 一次只能持有一个字段；设置新变体会清除前一个。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| google.golang.org/protobuf 1.30+ | active | — | — |

## 解决方案

1. **** (95% 成功率)
   ```
   Use the generated oneof wrapper types and set exactly one variant:
switch kind {
case "text":
    msg.Payload = &pb.Event_Text{Text: value}
case "blob":
    msg.Payload = &pb.Event_Blob{Blob: value}
}
   ```
2. **** (90% 成功率)
   ```
   Validate with proto.CheckInitialized and inspect the active variant before sending:
if msg.GetText() != "" && msg.GetBlob() != nil {
    return status.Error(codes.InvalidArgument, "oneof conflict")
}
   ```

## 无效尝试

- **** — The oneof semantics still apply to the copy; only the last-set variant survives, silently dropping the first. (85% 失败率)
- **** — The message is in an inconsistent state and the wire encoding contains only one variant, causing downstream logic errors. (90% 失败率)
