Home  >  Article  >  Backend Development  >  I want to generate protobuf from cmd's proto file without using "option go_package"

I want to generate protobuf from cmd's proto file without using "option go_package"

王林
王林forward
2024-02-09 15:54:141063browse

我想从 cmd 的 proto 文件生成 protobuf,而不使用“option go_package ”

php Editor Strawberry, you mentioned that you want to generate protobuf from the cmd proto file without using "option go_package". In this case, you can try using other methods to generate protobuf files. There are tools that can help you achieve this, such as using plugins or custom scripts to automatically generate protobuf files. This way you can generate the required protobuf files by other means without relying on the "go_package" option. This way you can be more flexible with your needs.

Question content

I have to generate a protobuf file from a proto file that imports another proto file. Another requirement is that it must be generated by cmd (by not defining "option go_package" in the proto file).

Solution

Suppose you have 3 files foo.proto, poo.proto and zoo.proto. File foo.proto imports message structures from poo.proto and zoo.proto. You need to write the commands in a specific order, first put foo.proto (import from other) first, then poo.proto (import from other) and zoo .proto (import other) after.

This is the command to generate protobuf without defining the option go_package

protoc --proto_path=./ --go_out=./pb --go-grpc_out=./pb
--go_opt=mfoo.proto=./ --go-grpc_opt=mfoo.proto=./
--go_opt=mpoo.proto=./ --go-grpc_opt=mpoo.proto=./
--go_opt=mzoo.proto=./ --go-grpc_opt=mzoo.proto=./
foo.proto goo.proto zoo.proto

Please note the m before the file name, it will replace the option go_package

in the code

foo.proto

syntax = "proto3";
import "goo.proto";
import "zoo.proto";

service foo {
    rpc foorpc(poomessage) returns (zoomessage) {}
}

poo.proto

syntax = "proto3";

message poomessage {
    string body = 1;
}

zoo.proto

syntax = "proto3";

message ZooMessage {
    string body = 1;
}

The above is the detailed content of I want to generate protobuf from cmd's proto file without using "option go_package". For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete