英文:
grpc: non-exported method and is defined in a different package
问题
更新:看起来这是一个Goland的问题。
找到了使用grpc一元调用的示例。
由于某种原因,无法实现Server结构体,你知道我为什么会得到这个错误吗:
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){};
}
英文:
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){};
}
答案1
得分: 2
如果你在 Goland 中遇到这个问题,只需在 Goland 中删除自动生成的接口实现:
func (*Server) mustEmbedUnimplementedGreetServiceServer() {
//TODO implement me
panic("implement me")
}
但你需要定义:
greetpb.UnimplementedGreetServiceServer
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论