新版本的protoc使用grpc容易遇到的两个坑,gen gRPC,mustEmbedUnimplementedHelloServer

–go_out: protoc-gen-go: plugins are not supported; use ‘protoc --go-grpc_out=…’ to generate gRPC

这是因为你安装的是更新版本的protoc-gen-go,但是你却用了旧版本的生成命令。

但是这两种方法都是可以完成目标的,只不过api不太一样。推荐基于Google版本的protoc-gen-go进行示范。

protoc -I . --go_out=. --go-grpc_out=. ./hello.proto

至于其他更详细的资料,你可以在这里看到:https://github.com/protocolbuffers/protobuf-go/releases/tag/v1.20.0#v1.20-generated-code

mustEmbedUnimplementedHelloServer编译报错

 ⚡ 09/15|12:00:08  server  go build
# test/grpc/hello/server
./main.go:39:28: cannot use HelloService (variable of type helloService) as type hello.HelloServer in argument to pb.RegisterHelloServer:
        helloService does not implement hello.HelloServer (missing mustEmbedUnimplementedHelloServer method)

查询结果: https://github.com/grpc/grpc-go/issues/3794

解决方案一,结构实现多加一个必须加的结构体

// 定义helloService并实现约定的接口
type helloService struct {
	pb.UnimplementedHelloServer // 必须加的结构体
}

解决方案二,生成就去掉掉helloService

protoc --go-grpc_out=require_unimplemented_servers=false[,other options...]:. can solve this problem.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值