a way to search for products by keywords. // - Each RPC may return Internal but it is not listed in the [ERRORS] section // for brevity. service SearchService { // Returns a list of suggestions for search keyword. rpc ListSuggestions(ListSuggestionsRequest) returns (ListSuggestionsResponse); } // `ListSuggestions` returns a list of suggestions and the count of results for // a search keyword. // - If the keyword is empty, an empty list is returned. // - If there are no suggestions for the search keywords, an empty list is // returned. // - The list of suggestions is returned in descending order of search count. message ListSuggestionsRequest { // Search keyword. string keyword = 1; // Limit of suggestions list. int32 limit = 15; }
// All implementations must embed UnimplementedSearchServiceServer // for forward compatibility type SearchServiceServer interface { // Returns a list of suggestions for search keyword. ListSuggestions(context.Context, *ListSuggestionsRequest) (*ListSuggestionsResponse, error) mustEmbedUnimplementedSearchServiceServer() } // UnimplementedSearchServiceServer must be embedded to have forward compatible implementations. type UnimplementedSearchServiceServer struct { } func (UnimplementedSearchServiceServer) ListSuggestions(context.Context, *ListSuggestionsRequest) (*ListSuggestionsResponse, error) { return nil, status.Errorf(codes.Unimplemented, "method ListSuggestions not implemented") } func (UnimplementedSearchServiceServer) mustEmbedUnimplementedSearchServiceServer() {} // UnsafeSearchServiceServer may be embedded to opt out of forward compatibility for this service. // Use of this interface is not recommended, as added methods to SearchServiceServer will // result in compilation errors. type UnsafeSearchServiceServer interface { mustEmbedUnimplementedSearchServiceServer() } func RegisterSearchServiceServer(s grpc.ServiceRegistrar, srv SearchServiceServer) { s.RegisterService(&SearchService_ServiceDesc, srv) }
srv) } // https://github.com/grpc/grpc-go/blob/master/server.go // ServiceRegistrar wraps a single method that supports service registration. It // enables users to pass concrete types other than grpc.Server to the service // registration methods exported by the IDL generated code. type ServiceRegistrar interface { // RegisterService registers a service and its implementation to the // concrete type implementing this interface. It may not be called // once the server has started serving. // desc describes the service and its methods and handlers. impl is the // service implementation which is passed to the method handlers. RegisterService(desc *ServiceDesc, impl interface{}) }
up a connection to the server. conn, err := grpc.Dial(*addr, grpc.WithTransportCredentials(insecure.NewCredentials())) if err != nil { log.Fatalf("did not connect: %v", err) } defer conn.Close() c := pb.NewGreeterClient(conn) // Contact the server and print out its response. ctx, cancel := context.WithTimeout(context.Background(), time.Second) defer cancel() r, err := c.SayHello(ctx, &pb.HelloRequest{Name: *name}) if err != nil { log.Fatalf("could not greet: %v", err) } log.Printf("Greeting: %s", r.GetMessage()) }
} type searchServiceClient struct { cc grpc.ClientConnInterface } // https://github.com/grpc/grpc-go/blob/master/clientconn.go // ClientConnInterface defines the functions clients need to perform unary and // streaming RPCs. It is implemented by *ClientConn, and is only intended to // be referenced by generated code. type ClientConnInterface interface { // Invoke performs a unary RPC and returns after the response is received // into reply. Invoke(ctx context.Context, method string, args interface{}, reply interface{}, opts ...CallOption) error // NewStream begins a streaming RPC. NewStream(ctx context.Context, desc *StreamDesc, method string, opts ...CallOption) (ClientStream, error) }