I am using gRPC/protobufs as the protocol to communicate between my client and server, both written in go. I'm able to run the command show below to generate the cards.pb.go
(server) and cards_grpc.pb.go
(client) files without any problem. The server file is working perfectly, without any issues. The client file, however, does not seem to have access to the message
items that I have defined within my cards.proto
file. My services, as well as my client code, require the defined message struct in order to call the service methods, but I'm not sure what I'm missing.
Here is the command I'm running:
protoc -I="./protos" \
--go_out=plugins=grpc:./server \
--go-grpc_out=./client \
protos/*.proto
Here is my project file structure:
|-- client
|-- protos (generated protobufs for client)
|-- cards_grpc.pb.go (this compiled successfully, but structs representing my messages cannot be found)
|-- protos (This is where the proto files are defined)
|-- cards.proto
|-- server
|-- protos (generated protobufs for server)
|-- cards.pb.go (this is working perfectly, has compiled services and messages)
Note: I have defined option go_package = "./protos";
in my cards.proto
file, which is why the generated files have outputted into */protos/*.pb.go
locations
So you are not generating any protobuf related code for the client code here, only gRPC one. In order to generate the structure that you are looking for, use the following command:
The
--go_out
generates the go code for protobuf and the--go-grpc_out
generates the go code for gRPC.Another thing,
--go_out=plugins=grpc
are not supported in go anymore. You should use the--go-grpc_out
.More recommendations
I highly recommend to share the proto directory with both the client and the server (if possible), this limits the potential error due to unsynchronised Proto files.
So you would have something like:
and then both access the files needed.
Second, if you are working with Go modules, I recommend that you use the
go_package
as following:and then generate the code like this:
Notice the
.
for the--go_out
and--go-grpc_out
. This maps the root of your project to the module name and this will generate the code inside yourprotos
directory by removing the Module name to thego_package
option. Then you will be able to access this generated code in your code like so:Clarification
Just to be clear about the
go_package
, you need to understand one thing: the protobuf package and thego_package
are not the same thing, the former defines package only usable in .proto files, the latter defines thepackage ...
inside your go files. An example:For Protobuf package
file1.proto
file2.proto
For
go_package
go.mod
file1.proto (at location: ${ROOT}/protos)
generation
main.go