I am a new user of gRPC in Go.
I have completed my code about client streaming API. And now I have a question.
With my knowledge, we must define message struct in proto file, then the protoc based on this defined message struct to generate code. In my case, protoc generate Go code. But this process limit the client or the server reuses API. For example, with client-streaming API, firstly, the client use this API to send temperature data to server. Then the client must re-defind message struct if the client want to send GPS coordinates to server, because the contruct of temperature is different from the contruct of GPS coordinates. But for the same purpose is send data to server.
syntax = "proto3";
package calculator;
option go_package="calculatorpb";
message TemperatureRequest{
float num =1;
}
message TemperatureResponse{
float result =1;
}
message CoordinatesRequest{
float long =1;
float lat =1;
}
message CoordinatesResponse{
float result =1;
}
service CalculatorService{
rpc Temperature(stream AverageRequest) returns (AverageResponse){} //client streaming for temperature
rpc Coordinates(stream CoordinatesRequest) returns (CoordinatesResponse){} //client streaming for Coordinates
}
It seem be inconvenient.
So, how the client can use dynamic message struct with gRPC in go?
If yes, please give me an example of the client-streaming API.
Use the Any
message type:
syntax = "proto3";
import "google/protobuf/any.proto";
message Example {
string id = 1;
google.protobuf.Any message = 2;
}
With Any
, you can use any user-defined proto message, also you need to share the new proto message with the client using some common repo or registry.