我编写了一个简单的GRPC服务器和一个客户端来调用服务器(都在Go中)。请告诉我是否使用golang / protobuf / struct是使用GRPC发送动态JSON的最佳方法。在下面的示例中,我之前是将Details创建为map [string] interface {}并将其序列化。然后,我在protoMessage中将其作为字节发送,并在服务器端反序列化该消息。
这是最好/最有效的方法,还是我应该在原型文件中将Details定义为结构?
下面是User.proto文件
syntax = "proto3";
package messages;
import "google/protobuf/struct.proto";
service UserService {
rpc SendJson (SendJsonRequest) returns (SendJsonResponse) {}
}
message SendJsonRequest {
string UserID = 1;
google.protobuf.Struct Details = 2;
}
message SendJsonResponse {
string Response = 1;
}
Run Code Online (Sandbox Code Playgroud)
下面是client.go文件包的主要导入文件(“上下文”“标志” pb“ grpc-test / messages / pb”“ log”
"google.golang.org/grpc"
)
func main() {
var serverAddr = flag.String("server_addr", "localhost:5001", "The server address in the format of host:port")
opts := []grpc.DialOption{grpc.WithInsecure()}
conn, err := grpc.Dial(*serverAddr, …Run Code Online (Sandbox Code Playgroud)