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

时间:2025-03-02 08:49:12

–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=. ./

至于其他更详细的资料,你可以在这里看到:/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
./:39:28: cannot use HelloService (variable of type helloService) as type  in argument to :
        helloService does not implement  (missing mustEmbedUnimplementedHelloServer method)

查询结果: /grpc/grpc-go/issues/3794

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

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

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

protoc --go-grpc_out=require_unimplemented_servers=false[,other options...]:. can solve this problem.