# proto：字段 "pb.Config.Timeout" 已在 oneof "pb.Config.value" 中设置

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

## 根因

同一消息上的 oneof 字段被赋值两次。在 google.golang.org/protobuf 中，设置 oneof 字段会清除其他字段，但通过反射对同一字段设置两次可能在某些生成代码路径中触发此错误。

## 版本兼容性

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

## 解决方案

1. **** (95% 成功率)
   ```
   Use the generated oneof wrapper types explicitly:
cfg := &pb.Config{}
cfg.Value = &pb.Config_Timeout{Timeout: 5 * time.Second}
   ```
2. **** (85% 成功率)
   ```
   Check which variant is set before assigning:
if _, ok := cfg.Value.(*pb.Config_Timeout); !ok {
    cfg.Value = &pb.Config_Timeout{Timeout: d}
}
   ```

## 无效尝试

- **** — proto.Reset clears all fields, losing unrelated data you already populated. (70% 失败率)
- **** — The panic leaves the message in an inconsistent state; subsequent marshaling may produce corrupt output. (80% 失败率)
