grpc: 非导出方法,并且在不同的包中定义

huangapple go评论105阅读模式
英文:

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

huangapple
  • 本文由 发表于 2022年7月2日 21:04:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/72839260.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定