将由protoc生成的客户端和服务器分开。

huangapple go评论153阅读模式
英文:

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 = &quot;proto3&quot;;

option go_package = &quot;github.com/&lt;username&gt;/&lt;myservice&gt;/pkg/grpc&quot;;

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/&lt;name&gt;.pb.go files containing models and pkg/grpc/&lt;name&gt;_grpc.pb.go files containing server interface and client implementation (picture)

将由protoc生成的客户端和服务器分开。

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 is libprotoc 3.19.0
  • protoc-gen-go-grpc version is protoc-gen-go-grpc 1.1.0
  • protoc-gen-go version is protoc-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 flags
  • protoc-gen-go-grpc supports --go-grpc_out and --go-grpc_opt flags

huangapple
  • 本文由 发表于 2021年10月29日 17:34:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/69766638.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定