A bit of context
I have been recently learning about using gRPC in Go. I have been liking it so far! The notion of using protocol buffers and HTTP 2.0 is exciting to me. I find it cool to make remote calls on another service as if they were locally called in the same memory space. Being able to understand and apply gRPC is one of my goals in learning about synchronous communication among distributed systems. Notably, gRPC is an alternative method to using HTTP REST for synchronously communicating among services.
Using a .protofile
, we can define our service and specify the methods that can be remotely called. We also need to explicitly specify the parameter and return types of each method. Take a look from the helloworld.proto
example of grpc-go:
Above is a simple greeter service that you can say “Hello” to. You need to specify a name you would greet (as specified in the HelloRequest
message type) and the service shall reply to you.
You can then feed this .proto
file into the protocol buffer compiler (protoc
) so you can enjoy auto-generated data access classes of your preferred language. Here is an example of converting helloworld.proto
to Go code:
A slight caveat
In regards to gRPC’s development experience, I do not like typing long commands every time I want to compile my .proto
files into Go code. I like using Makefiles and we can use it again here!
How to make it more convenient
You can use a Makefile
to compile all .proto
files in your Go project. Here is an example of the Makefile:
In the example above, I placed my .proto
files in pkg/**/grpc/*.proto
directories. It is derived from the Go “standard” project layout. You can modify the directory to your liking. After running make protoc
, all the Go code would be generated adjacent to the .proto
files (the paths=source_relative
flag specifies that).
Hooray, no need to individually type commands for each .proto
file again!