goencapsulationprotocgrpc-gogenerated-code

Separate client and server generated by protoc


I am trying to have protoc-generated server interface and client implementation in separate packages

The header part of my .proto files is the following:

syntax = "proto3";

option go_package = "github.com/<username>/<myservice>/pkg/grpc";

And I am using this command to generate .go files:

protoc --go_out=. --go_opt=paths=source_relative\
        --go-grpc_out=. --go-grpc_opt=paths=source_relative\
        pkg/grpc/*.proto

It generates pkg/grpc/<name>.pb.go files containing models and pkg/grpc/<name>_grpc.pb.go files containing server interface and client implementation (picture)

enter image description here

But I want the server to go to, say internal/pkg/grpc/, while the client and the models remaining inside pkg/grpc/, and the server correctly importing the models.

Versions:

I am new to golang and protobuf, so if whatever I am asking happens to be bad practice, feel free to point me to the idiomatic one


Solution

  • It seems there isn't an option to do this. The plugin protoc-gen-go-grpc writes the output service code to the same file with _grpc.pb.go suffix, where "service" includes both client and server code.

    You can only define different output paths per plugin: