Home > Backend Development > Golang > 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'

王林
Release: 2024-02-09 15:54:14
forward
1138 people have browsed it

我想从 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
Copy after login

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) {}
}
Copy after login

poo.proto

syntax = "proto3";

message poomessage {
    string body = 1;
}
Copy after login

zoo.proto

syntax = "proto3";

message ZooMessage {
    string body = 1;
}
Copy after login

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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template