protobuf with Go not able to compile to the desired folder with paths=source_relative

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

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

huangapple
  • 本文由 发表于 2022年6月30日 20:40:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/72816024.html
匿名

发表评论

匿名网友

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

确定