Compile all Proto files to Go code using a Makefile

Photo by Juan Gomez on Unsplash

Compile all Proto files to Go code using a Makefile

We like being lazy (and smart)

ยท

2 min read

Table of contents

Foreword

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:

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:

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

Problem

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!

Solution

You can use a Makefile to compile all .proto files in your Go project. Here is an example of the 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!

That is all for this article. Cheers! ๐Ÿ˜Ž

ย