Building Go with protobufs

Hi,

I am newbie with golang and I wanted to know if there is a guideline how to integrate non-go tools into go build ecosystem. Specifically, if I need to generate go files from proto file but I do not want to do it manually every time I change my proto file is there a way to run custom build rules inside go-build system? What’s the simplest and most common approach?

Thanks!

Makefiles, shell scripts or Go scripts, depending on preferences.

Typically you’d generate code from protobufs manually-ish when the definitions change (using “go generate” or “make protobuf” or something) and check in the generated code. There’s no facility for custom commands triggered by “go build”.

2 Likes

Hi,

This sound like a job for the go generate tool.

Basically the idea is to add a file in the folder where your .proto file is and name it something like generate.go.

Source tree would look something like this:

|_ proto
      |_ my_protobuf_file.proto
      |_ generate.go

In there you add the command the go generate tool need to execute on the first line and comment it out.

The rest of the file is just the package declaration

//go:generate protoc my_protobuf_file.proto --go_out=plugins=grpc:.

package proto

When you build your app you call go generate tool as well on the package where the .proto file is.

###References:###
Blog post
go generate Design document

3 Likes

Thanks!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.