Chris G.
Chris G.

Reputation: 25934

grpc: non-exported method and is defined in a different package

Update: It looks like it is a Goland issue.

Found this example of using grpc unary.

For some reason, the Server struct can not be implemented, do you know why I am gettign this error:

Cannot use '&Server{}' (type *Server) as the type GreetServiceServer Type cannot implement 'GreetServiceServer' as it has a non-exported method and is defined in a different package

main.go

package main

import (
    "context"
    "log"
    "net"

    greetpb "example.com/s1/pb"
    "google.golang.org/grpc"
)

type Server struct {
    greetpb.UnimplementedGreetServiceServer
}

func (*Server) mustEmbedUnimplementedGreetServiceServer() {
    //TODO implement me
    panic("implement me")
}

// Greet greets with FirstName
func (*Server) Greet(ctx context.Context, in *greetpb.GreetRequest) (*greetpb.GreetResponse, error) {
    result := "Hello " + in.GetGreeting().GetFirstName()
    res := greetpb.GreetResponse{
        Result: result,
    }
    return &res, nil
}

func main() {
    lis, err := net.Listen("tcp", "0.0.0.0:50051")
    if err != nil {
        log.Fatalf("Failed to listen %v", err)
    }

    s := grpc.NewServer()
    greetpb.RegisterGreetServiceServer(s, &Server{})

    if err := s.Serve(lis); err != nil {
        log.Fatalf("Failed to start server %v", err)
    }
}

greet.proto

syntax = "proto3";

package greet;
option go_package="greetpb";

message Greeting {
  string firstName = 1;
  string lastName = 2;
}

message GreetRequest {
  Greeting greeting = 1;
}

message GreetResponse {
  string result = 1;
}

service GreetService{
  rpc Greet(GreetRequest) returns (GreetResponse){};
}

Upvotes: 1

Views: 2171

Answers (1)

Chris G.
Chris G.

Reputation: 25934

If you encounter this in Goland, just remove auto-generated interface implementation in Goland:

func (*Server) mustEmbedUnimplementedGreetServiceServer() {
    //TODO implement me
    panic("implement me")
}

But you will need to define:

greetpb.UnimplementedGreetServiceServer

Upvotes: 3

Related Questions