英文:
Separate client and server generated by protoc
问题
我正在尝试将由protoc生成的服务器接口和客户端实现放在不同的包中。
我的.proto
文件的头部如下所示:
syntax = "proto3";
option go_package = "github.com/<username>/<myservice>/pkg/grpc";
我使用以下命令生成.go
文件:
protoc --go_out=. --go_opt=paths=source_relative\
--go-grpc_out=. --go-grpc_opt=paths=source_relative\
pkg/grpc/*.proto
它会生成包含模型的pkg/grpc/<name>.pb.go
文件,以及包含服务器接口和客户端实现的pkg/grpc/<name>_grpc.pb.go
文件(图片)。
但是我希望服务器放在internal/pkg/grpc/
目录下,而客户端和模型仍然保留在pkg/grpc/
目录中,并且服务器能够正确导入模型。
版本信息:
protoc
版本为libprotoc 3.19.0
protoc-gen-go-grpc
版本为protoc-gen-go-grpc 1.1.0
protoc-gen-go
版本为protoc-gen-go v1.27.1
我对golang和protobuf都不太熟悉,所以如果我提出的任何问题都不符合最佳实践,请随时指出正确的做法。
英文:
I am trying to have protoc-generated server interface and client implementation in separate packages
The header part of my .proto
files is the following:
syntax = "proto3";
option go_package = "github.com/<username>/<myservice>/pkg/grpc";
And I am using this command to generate .go
files:
protoc --go_out=. --go_opt=paths=source_relative\
--go-grpc_out=. --go-grpc_opt=paths=source_relative\
pkg/grpc/*.proto
It generates pkg/grpc/<name>.pb.go
files containing models and pkg/grpc/<name>_grpc.pb.go
files containing server interface and client implementation (picture)
But I want the server to go to, say internal/pkg/grpc/
, while the client and the models remaining inside pkg/grpc/
, and the server correctly importing the models.
Versions:
protoc
version islibprotoc 3.19.0
protoc-gen-go-grpc
version isprotoc-gen-go-grpc 1.1.0
protoc-gen-go
version isprotoc-gen-go v1.27.1
I am new to golang and protobuf, so if whatever I am asking happens to be bad practice, feel free to point me to the idiomatic one
答案1
得分: 2
似乎没有这样的选项。插件protoc-gen-go-grpc
将输出的服务代码写入具有_grpc.pb.go
后缀的同一文件中,其中“服务”包括客户端和服务器代码。
您只能为每个插件定义不同的输出路径:
protoc-gen-go
支持--go_out
和--go_opt
标志protoc-gen-go-grpc
支持--go-grpc_out
和--go-grpc_opt
标志
英文:
It seems there isn't an option to do this. The plugin protoc-gen-go-grpc
writes the output service code to the same file with _grpc.pb.go
suffix, where "service" includes both client and server code.
You can only define different output paths per plugin:
protoc-gen-go
supports--go_out
and--go_opt
flagsprotoc-gen-go-grpc
supports--go-grpc_out
and--go-grpc_opt
flags
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论