go runtime_error ai_generated true

panic: proto: 字段 pb.User.labels 包含无效的 nil map 值

panic: proto: field pb.User.labels contains invalid nil map value

ID: go/protobuf-marshal-nil-map

其他格式: JSON · Markdown 中文 · English
80%修复率
84%置信度
0证据数
2025-08-25首次发现

版本兼容性

版本状态引入弃用备注
google.golang.org/protobuf 1.x active — — —

根因分析

proto map 字段被赋值为包含 nil 值的 map(如 map[string]*Meta{"k": nil}),protobuf 运行时在序列化时拒绝该值。

English

A proto map field was assigned a map with a nil value (e.g. map[string]*Meta{"k": nil}), which the protobuf runtime rejects during marshal.

generic

解决方案

  1. 92% 成功率
    labels := make(map[string]*pb.Meta)
    for k, v := range src {
      if v == nil { continue }
      labels[k] = v
    }
    msg.Labels = labels
  2. 88% 成功率
    // user.proto
    message User {
      map<string, Meta> labels = 1;
    }

无效尝试

常见但无效的做法:

  1. 85% 失败

    Swallowing the panic leaves the message unsent and corrupts control flow.

  2. 70% 失败

    Data loss and hidden bugs; the caller has no signal that data was dropped.