How to represent golang "any" type in protobuf language

793 Views Asked by At

am using a protobuf definition file to create golang classes

file.proto

message my_message {
 <what type to use?> data = 1 [json_name = "data"]
}

generated.go

type MyMessage struct {
  data any
}

I have checked out Struct , Value and Any, though none of them actually mark the type as “any” in generated classes.

The closes I get is Value type which can accomodate primitives as well as structs and lists.

1

There are 1 best solutions below

2
nj_ On

As far as I'm aware the Go code generator doesn't have any circumstances where any/interface{} will be used for a generated field.

google.protobuf.Any is likely what you want here, but the field in the generated code will of type anypb.Any. If you absolutely require the any type be used, you'll unfortunately have to manually map to another struct.