Introduction

Most apps we make need a means of communication. We usually use JSON, or just plain text. JSON has got especially popular because of the rise of Node.js. The truth though, is, that JSON isn’t really a fast format. The marshaller in Go also isn’t that fast. That’s why in this article we’ll learn how to use google protocol buffers. They are in fact very easy to use, and are much faster than JSON.

Regarding the performance gains, here they are, according to this benchmark:

benchmark

iter

time/iter

bytes alloc

allocs

BenchmarkJsonMarshal-8

500000

3714 ns/op

1232 B/op

10 allocs/op

BenchmarkJsonUnmarshal-8

500000

4125 ns/op

416 B/op

7 allocs/op

BenchmarkProtobufMarshal-8

1000000

1554 ns/op

200 B/op

7 allocs/op

BenchmarkProtobufUnmarshal-8

1000000

1055 ns/op

192 B/op

10 allocs/op

BenchmarkGogoprotobufMarshal-8

10000000

211 ns/op

64 B/op

1 allocs/op

BenchmarkGogoprotobufUnmarshal-8

5000000

289 ns/op

96 B/op

3 allocs/op

Ok, now let’s set up the environment.

Setup

First we’ll need to get the protobuffer compiler binaries from here:
https://github.com/google/protobuf/releases/tag/v3.0.0-beta-3
Unpack them somewhere in your PATH.

The next step is to get the golang plugin. Make sure that GOPATH/bin is in your PATH.

go get -u github.com/golang/protobuf/protoc-gen-go

Writing .proto files

First we need to define the version of protobuffers we will use. Here we will use the newest – proto3. We’ll also define the package of the file. This will also be our go package name of the generated file.

syntax = "proto3";
package main;

Ok, now we’ll define our main structure in the file. The Client structure:

message Client {
}

Now it’s time to define the available fields. Fields are refered to by id, so for each field we define the type, name and id like this:

Note that we used bytes.NewBuffer, so our raw data satisfies the Reader requirement for the request body.

Conclusion

As you can see, protobuffs are really easy to use and provide an actual speed boost in your application. Hope you’ll try to use them instead of JSON or other forms of transport in your next project. You can get more information about the more advanced functionalities here: https://developers.google.com/protocol-buffers/docs/gotutorial