英文:
protobuf with Go not able to compile to the desired folder with paths=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
文件中模块的名称是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;
}
我的命令或者.proto
文件有什么问题吗?
英文:
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?
答案1
得分: 2
只需删除--go_opt=paths=source_relative
。
如果你希望生成的文件放在./pb
目录下,那么选项--go_out=./pb/
已经指定了正确的输出路径。
通过添加source_relative
,输出文件将与源文件夹中的文件位置相对应(文档):
如果指定了
paths=source_relative
标志,输出文件将放置在与输入文件相同的相对目录中。例如,输入文件protos/buzz.proto
将生成一个位于protos/buzz.pb.go
的输出文件。
由于你的源文件位于./proto
文件夹中,使用source_relative
选项,输出文件也将位于./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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论