skip to content
jgarivera.com

Compile all Proto files to Go code using a Makefile

/ 2 min read

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:

helloworld.proto
syntax = "proto3";
option go_package = "google.golang.org/grpc/examples/helloworld/helloworld";
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}

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:

Terminal window
protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative helloworld.proto

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:

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

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!

The discussion for this post can be found here as well.