go type_error ai_generated true

error: proto: cannot use map field "metadata" with key type string: unsupported key type

ID: go/grpc-protobuf-map-key-type-error

Also available as: JSON · Markdown · 中文
80%Fix Rate
81%Confidence
0Evidence
2024-09-18First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.0 active

Root Cause

The protobuf map field uses an unsupported key type (e.g., float or bytes) in Go generated code.

generic

中文

protobuf映射字段在Go生成的代码中使用了不支持的键类型(例如float或bytes)。

Workarounds

  1. 95% success Use string keys in proto definition.
    map<string, string> metadata = 1;
  2. 85% success Encode unsupported keys as string with custom encoding.
    map<string, string> metadata = 1; // encode float as string via strconv.FormatFloat

Dead Ends

Common approaches that don't work:

  1. Change the key type in generated Go code manually. 95% fail

    Regeneration will overwrite manual changes.

  2. Use a repeated field instead of map. 80% fail

    Requires changing the proto schema and all consumers.