Chapter 10. Binary Recipes

10.0 Introduction

So far our I/O-related recipes have been working with text data like in CSV or JSON. This is good because this data is meant to be read by humans as well as machines. However, verbose text data formats can sometimes be a disadvantage.

Speed, of course, is one consideration—it’s faster to transfer less data and being less verbose helps. With the proliferation of Internet of Things (IoT) devices and sensors, we often need to resort to low-bandwidth networks to send data. Memory and storage space is the other consideration. Smaller devices and sensors, often powered by batteries, mean that you cannot afford to use a lot of memory or storage for the data.

All this comes down to compacting data formats to the bit and byte levels. There are many such formats in existence already—storing data in smaller sizes has always been a necessity for past computing. Some recently popular formats include BSON (Binary JSON), Protocol Buffers/protobuf from Google, and Apache Thrift from Facebook.

In this chapter, you’ll go through Go’s binary format, gob. You’ll also build your custom binary format using the encoding/binary package. In both cases, this chapter discusses how you can encode, store, and decode these formats.

10.1 Encoding Data to gob Format Data

Problem

You want to encode structs into binary gob format.

Solution

Use the encoding/gob package to encode the structs into bytes that can be stored or sent elsewhere.

Discussion

The encoding/gob ...

Get Go Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.