Type cannot implement 'X' because it has non-exported methods and is defined in a different package

WBOY
Release: 2024-02-09 11:10:10
forward
444 people have browsed it

Type cannot implement X because it has non-exported methods and is defined in a different package

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!

Question content

I have this code:

s := grpc.newserver()
pb.registermessageserviceserver(s, &messageserver{})
Copy after login

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
Copy after login

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
}
Copy after login

The error is in this line:

pb.RegisterMessageServiceServer(s, &messageServer{}) // here
Copy after login

Not sure what happened :(

Workaround

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template