当前位置: 首页> 文旅> 酒店 > grpc教程——proto文件转go

grpc教程——proto文件转go

时间:2025/7/9 21:09:03来源:https://blog.csdn.net/notfindjob/article/details/139833027 浏览次数:0次

【1】编写一个proto文件

syntax = "proto3";
package myproto;service NC{rpc SayStatus (NCRequest) returns (NCResponse){}
}message NCRequest{	string name = 1;
}
message NCResponse{string status =1;
}

【2】转换:protoc --go_out=. myservice.proto(其他语言:如python,将go换成python,c++换成

--cpp_out)

protoc --go_out=plugins=grpc:. myservice.proto

报错:

【3】解决问题:

1)下载protoc.exe,添加windows环境path,protoc --version   (图三)

图一、

图二、

Release Protocol Buffers v27.1 · protocolbuffers/protobuf · GitHub

图三、

另外的下载路径:

https://download.csdn.net/download/notfindjob/89460870

2)因为protoc不支持go语言,所以需要下载插件,下载如下两个文件protoc-gen-go.exe、protoc-gen-go-grpc.exe

这两个文件的作用是转换为pb.go和grpc.pb.go文件

下载方法1:

go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest

结果如下:

下载方法2:

https://download.csdn.net/download/notfindjob/89460383?spm=1001.2014.3001.5503

https://download.csdn.net/download/notfindjob/89460283?spm=1001.2014.3001.5503

【4】protoc-gen-go: unable to determine Go import path for "myservice.proto"

解决办法:

//协议为proto3
syntax = "proto3";
// 指定生成的Go代码在你项目中的导入路径
option go_package="./;myproto";
//协议为proto3
syntax = "proto3";
// 指定生成的Go代码在你项目中的导入路径
option go_package="./;myproto"; //注意:myproto会变成转换后的包名称
package myproto;
// 定义服务接口
// 可定义多个服务,每个服务可定义多个接口
service CNC{rpc SayStatus (CNCRequest) returns (CNCResponse){}
}
// 请求参数结构
message CNCRequest{	string name = 1;
}
// 响应参数结构
message CNCResponse{string status =1;
}

【5】--go_out: protoc-gen-go: plugins are not supported

解决办法:

1)

protoc --go_out=. myservice.proto

2)

protoc --go-grpc_out=. myservice.proto

--go_out:指定 xx.pb.go 文件的生成位置(c++ 换成--cpp_out)

--go-grpcout:指定 xx_grpc.pb.go 文件的生成位置(c++换成--grpcout)

xx.proto:指定了需要处理的pb 文件

关键字:grpc教程——proto文件转go

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: