Home > other >  Separate client and server generated by protoc
Separate client and server generated by protoc

Time:11-02

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:

  • protoc version is libprotoc 3.19.0
  • protoc-gen-go-grpc version is protoc-gen-go-grpc 1.1.0
  • protoc-gen-go version is protoc-gen-go v1.27.1

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

CodePudding user response:

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:

  • protoc-gen-go supports --go_out and --go_opt flags
  • protoc-gen-go-grpc supports --go-grpc_out and --go-grpc_opt flags
  • Related