Protocol Buffers and Go
Overview
This post explains explains how to use protocol buffers in go from installing protocol buffer to actually using one.
Install Protocol Buffer
First of all, protocol buffer compiler protoc
need to be installed.
Install Protocol Buffer Go Plugin
After installing protocol buffer compiler protoc
, we need to install protocol
buffer go plugin to generate go source files and a library for runtime support.
This software has two parts: a ‘protocol compiler plugin’ that generates Go source files that, once compiled, can access and manage protocol buffers; and a library that implements run-time support for encoding (marshaling), decoding (unmarshaling), and accessing protocol buffers.
Use Protocol Buffer
Now we are ready to start using protocol buffer. Define a protocol buffer
message and save it as person.proto
Side note: The official doc warns that required
keyword must be treaded
carefully.
Protocol Buffers Language Guide
Required Is Forever You should be very careful about marking fields as required. If at some point you wish to stop writing or sending a required field, it will be problematic to change the field to an optional field – old readers will consider messages without this field to be incomplete and may reject or drop them unintentionally. You should consider writing application-specific custom validation routines for your buffers instead. Some engineers at Google have come to the conclusion that using required does more harm than good; they prefer to use only optional and repeated. However, this view is not universal.
Next generate go source files from person.proto
. You should see a generated
file named person.pb.go
.
Finally try marshalling and unmarshalling the person message in main.go
.
In (1) you can see that p1.Gender
returns nil
(because it is raw property
of person object) but p1.GetGender()
returns default value Male which is
because GetGender()
is a getter method generated by protocol buffer. In (2)
you can see that p1 and p2 have the same value.
Thanks for reading, happy hacking!