Home > Back-end >  Is there a way to import generated structs in grpc.pb.go during protoc compilation?
Is there a way to import generated structs in grpc.pb.go during protoc compilation?

Time:06-11

I am going to specify different out directories for generated structs from proto messages and generated interfaces from grpc service.

I achieve that with this two flags:

--go_out=internal/proto
--go-grpc_out=internal/grpc

After generating the internal/proto package is not imported in mymodel_grpc.pb.go file, but it would be very convenient if there is any flag to do that automatically during compilation.

I assume --go-grpc_opt flag could have such an option, but I couldn't find any.

Any experience with this kind of scenario is interesting.

CodePudding user response:

If I understand correctly, you would like to generate the protobuf messages in one package and the service bindings in another package.

Protobuf files include a package directive and this can be bound to options directives e.g. for Go(lang) using go_package=. The binding is 1:1; one package (and its messages|services) are bound to one e.g. Go package.

NOTE Protobuf supports multiple languages too and so it must provide generic solutions that can be applied once (in Protobuf file) but used many (in each language's generated code).

So, I think your solution is to separate your messages and services into different packages (with different options and go_package specifications. Then, protoc can be guided to generated these to separate Golang packages.

  • Related