service HelloWorldService { rpc SayHelloWorld(HelloWorldRequest) returns (HelloWorldResponse){} }
那么接下来,我们先创建一个服务端。
创建服务端
在生成的hello_world.pb.go中,已经为我们生成了服务端的接口:
1 2 3 4
// HelloWorldServiceServer is the server API for HelloWorldService service. type HelloWorldServiceServer interface { SayHelloWorld(context.Context, *HelloWorldRequest) (*HelloWorldResponse, error) }
var an *any.Any if in.Infos["hello"] == "world" { an, err = ptypes.MarshalAny(&pb.HelloWorld{Msg: "Good Request"}) } else { an, err = ptypes.MarshalAny(&pb.Error{Msg: []string{"Bad Request", "Wrong Info Msg"}}) }
if err != nil { return } return &pb.HelloWorldResponse{ Reply: "Hello World !!", Details: []*any.Any{an}, }, nil }