php Editor Yuzi, hello! Regarding the issue of implementing the "X" type, since it has non-exported methods and is defined in a different package, it cannot be implemented directly. In PHP, methods and properties between different packages cannot be directly accessed. If you want to implement the "X" type, you can consider using interfaces or inheritance to indirectly implement the required functionality. Get the required methods and properties by defining an interface and implementing it in different classes, or by inheriting. This enables "X" type functionality to be implemented in different packages. Hope this helps!
I have this code:
s := grpc.newserver() pb.registermessageserviceserver(s, &messageserver{})
I have this error:
cannot use '&messageserver{}' (type *messageserver) as the type messageserviceserver type cannot implement 'messageserviceserver' as it has a non-exported method and is defined in a different package
My messageserver structure looks like this:
type messageserver struct{} func (s *messageserver) mustembedunimplementedmessageserviceserver() { //todo implement me panic("implement me") } func (s *messageserver) mustembedunimplementedmessageserviceserver() { //todo implement me panic("implement me") } func (s *messageserver) sendmessage(ctx context.context, msg *pb.message) (*pb.response, error) { // write the message to kafka producer, err := sarama.newsyncproducer([]string{kafkabroker}, nil) if err != nil { log.fatalf("error creating kafka producer: %v", err) return nil, err } defer producer.close() kafkamsg := &sarama.producermessage{ topic: kafkatopic, value: sarama.stringencoder(msg.content), } _, _, err = producer.sendmessage(kafkamsg) if err != nil { log.printf("failed to send message to kafka: %v", err) return nil, err } return &pb.response{message: "message sent to kafka"}, nil }
The error is in this line:
pb.RegisterMessageServiceServer(s, &messageServer{}) // here
Not sure what happened :(
You should not provide an implementation of mustEmbedUnimplementedMessageServiceServer()
. You should make your server structure embedded UnimplementedMessageServiceServer
. The member is named so to tell you this. The interface also has a documentation comment that says
All implementations must embed UnimplementedMessageServiceServer for forward compatibility.
The idea is that one day, gRPC developers might add a new method to the server interface. If they do this, they will also add a new method to the unimplemented
type that returns a "method Blah not Implemented" error. Since you're forced to embed the unimplemented
type into your server type, you'll inherit the method and get a default implementation that throws an error, rather than having your code suddenly start failing type checks because your Server type is missing new methods.
The above is the detailed content of Type cannot implement 'X' because it has non-exported methods and is defined in a different package. For more information, please follow other related articles on the PHP Chinese website!