带有路径的Protobuf无法编译到所需的文件夹= source_relative

发布于 2025-02-11 17:36:39 字数 715 浏览 1 评论 0原文

我想生成.pb.go文件到./ pb文件夹。我在./ proto文件夹下有.proto文件。我在根文件夹中运行了此命令:

protoc -go_out =。/pb/-go_opt = paths = source_relative ./ proto/*。proto

但是,我的.go文件始终在<<代码> ./ pb/proto 而不是./ pb文件夹,像这样:

.
|____pb
| |____proto
| | |____my_message_one.pb.go
| | |____my_message_two.pb.go
| | |____my_message_three.pb.go

我的模块的名称go.mod is module grpc_tutorial,这是我的.proto文件的示例:

syntax = "proto3";

option go_package="./grpc_tutorial/pb"; 

message Screen {
  string some_message = 1;
  string some_message_two = 2;
  bool some_bool = 3;
}

我的命令或原始文件有什么问题吗?

I want to generate .pb.go files to the ./pb folder. I have my .proto files under ./proto folder. I ran this command in my root folder:

protoc --go_out=./pb/ --go_opt=paths=source_relative ./proto/*.proto

However, my .go files always end up under ./pb/proto instead of ./pb folder, like this:

.
|____pb
| |____proto
| | |____my_message_one.pb.go
| | |____my_message_two.pb.go
| | |____my_message_three.pb.go

The name of my module in go.mod is module grpc_tutorial and here's an example my .proto file:

syntax = "proto3";

option go_package="./grpc_tutorial/pb"; 

message Screen {
  string some_message = 1;
  string some_message_two = 2;
  bool some_bool = 3;
}

Is there anything wrong with my command or proto file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

以可爱出名 2025-02-18 17:36:39

只需删除-GO_OPT = PATHS = source_relative

如果要生成的文件最终以./ pb最终形式,则选项- go_out =。/pb/已经指定了正确的输出。

通过添加source_relative,输出镜像源文件夹中文件的处理(文档):

如果指定了paths = source_relative flag,则将输出文件放置在与输入文件相同的相对目录中。例如,输入文件protos/buzz.proto在protos/buzz.pb.go。

上产生输出文件

由于您的源文件位于./ proto文件夹中./ Proto文件夹。

整个输出依次在- go_out指定的标志的情况下,即在./ pb下结束。这就是为什么生成文件的路径看起来像./ pb/proto/my_message_one.pb.go,如您所示。

通过删除source_relative选项,输出不会相互关联到源文件夹,而直接进入./ pb

在这种情况下,您想要的命令只是:

protoc --go_out=./pb/ ./proto/*.proto

Just remove --go_opt=paths=source_relative.

If you want the generated files end up in ./pb, then the option --go_out=./pb/ already specifies the correct output.

By adding source_relative, the output mirrors the disposition of the files in the source folder (documentation):

If the paths=source_relative flag is specified, the output file is placed in the same relative directory as the input file. For example, an input file protos/buzz.proto results in an output file at protos/buzz.pb.go.

Since your source files are located inside the ./proto folder, with source_relative the output files also end up inside a ./proto folder.

That entire output in turn ends up right where the --go_out flag specified, i.e. under ./pb. This is why finally the path of a generated file looks like ./pb/proto/my_message_one.pb.go, as you showed.

By removing the source_relative option, the output instead is not relativized to the source folder and goes directly into ./pb.

The command you want in this case is just:

protoc --go_out=./pb/ ./proto/*.proto
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文