当前位置:网站首页>Protobuf Grpc使用异常 类型有未导出的方法,并且是在不同的软件包中定义
Protobuf Grpc使用异常 类型有未导出的方法,并且是在不同的软件包中定义
2022-08-03 18:55:00 【chutianxia】
Grpc使用异常
类型有未导出的方法,并且是在不同的软件包中定义
导致原因:
导出的pb.go 文件中 GretterServer interface中有私有变量导致的。
// GreeterServer is the server API for Greeter service. // All implementations must embed UnimplementedGreeterServer // for forward compatibility type GreeterServer interface { SayHello(context.Context, *HelloRequest) (*HelloReply, error) mustEmbedUnimplementedGreeterServer() }proto文件内容:
syntax = "proto3"; package helloworld; option go_package = "./helloworld"; service Greeter { rpc SayHello (HelloRequest) returns (HelloReply) { } } message HelloRequest { string name = 1; } message HelloReply { string message = 1; }异常提示:
无法将 ‘&GreeterServer{}’ (类型 *GreeterServer) 用作类型 GreeterServer 类型无法实现 ‘GreeterServer’,因为类型有未导出的方法,并且是在不同的软件包中定义

运行异常:
.\server.go:33:35: cannot use &GreeterServer{} (value of type *GreeterServer) as type helloworld.GreeterServer in argument to pb.RegisterGreeterServer:
*GreeterServer does not implement helloworld.GreeterServer (missing mustEmbedUnimplementedGreeterServer method)
protobuf 和 grpc 版本:
google.golang.org/grpc v1.48.0
google.golang.org/protobuf v1.28.0修复方式: 增加第15行代码内容:*pb.UnimplementedXxxxxServer Xxxxx为proto文件中定义的service名称
package main import ( pb "GoRpc/proto/helloworld" "context" "flag" "google.golang.org/grpc" "net" ) var port string type GreeterServer struct { // 增加此行代码 *pb.UnimplementedGreeterServer } func init() { flag.StringVar(&port, "p", "8001", "port to connect to") flag.Parse() } func (s *GreeterServer) SayHello(ctx context.Context, r *pb.HelloRequest) (*pb.HelloReply, error) { return &pb.HelloReply{ Message: "Hello.world"}, nil } func main() { server := grpc.NewServer() pb.RegisterGreeterServer(server, &GreeterServer{ }) lis, _ := net.Listen("tcp", ":"+port) server.Serve(lis) }修复问题的原因,查看helloworld_grpc.pb.go 文件。 实际SayHello由UnimplementedGreeterServer 结构实现
// GreeterServer is the server API for Greeter service. // All implementations must embed UnimplementedGreeterServer // for forward compatibility type GreeterServer interface { SayHello(context.Context, *HelloRequest) (*HelloReply, error) mustEmbedUnimplementedGreeterServer() } // UnimplementedGreeterServer must be embedded to have forward compatible implementations. type UnimplementedGreeterServer struct { } func (UnimplementedGreeterServer) SayHello(context.Context, *HelloRequest) (*HelloReply, error) { return nil, status.Errorf(codes.Unimplemented, "method SayHello not implemented") }
边栏推荐
猜你喜欢
随机推荐
实时渲染器不止lumion,Chaos Vantage你值得一试
How does MySQL permanently support Chinese input once and for all?
MySQL如何 drop 大表
【QT】入门心法
15、学习MySQL NULL 值处理
POJ 1465 Multiple(用BFS求能组成的n的最小倍数)
vulnhub pyexp: 1
谷歌浏览器安装插件教程步骤,开发用这2个插件工作效率倍增
[数据集][VOC]老鼠数据集voc格式3001张
SQL代码需要供其他人复用,为什么传统的复制代码不可靠?
高等数学---第十章无穷级数---常数项级数
Blender script 删除所有幽灵对象
爬虫之selenium
H.265网页播放器EasyPlayer获取视频流正常,但是播放出现黑屏是什么原因?
广告电商、泰山众筹、链动2+1,这3个模式到底怎么样?
LineSegmentTree线段树
PHP基础笔记-NO.2
智能合约安全——delegatecall (2)
pytest接口自动化测试框架 | 基于Pytest的Web UI自动化测试框架介绍
VsCode预览Geojson数据


![选出表中的中位数记录[构造左右边界 || 问题转换]](/img/02/8d8e515c994c8a1a364f1e299d73f7.png)






