带有路径的Protobuf无法编译到所需的文件夹= source_relative
我想生成.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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需删除
-GO_OPT = PATHS = source_relative
。如果要生成的文件最终以
./ pb
最终形式,则选项- go_out =。/pb/
已经指定了正确的输出。通过添加
source_relative
,输出镜像源文件夹中文件的处理(文档):由于您的源文件位于
./ proto
文件夹中./ Proto
文件夹。整个输出依次在
- go_out
指定的标志的情况下,即在./ pb
下结束。这就是为什么生成文件的路径看起来像./ pb/proto/my_message_one.pb.go
,如您所示。通过删除
source_relative
选项,输出不会相互关联到源文件夹,而直接进入./ pb
。在这种情况下,您想要的命令只是:
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):Since your source files are located inside the
./proto
folder, withsource_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: