Mahdi.Kh
September 21, 2026
We get familiar with Gob and learn how it can be used to convert data into binary format.
To transfer data over a network or store it in a file, it's best to encode the data into a specific format. There are many approaches, such as JSON, XML, and Protobuffer, but there's also another approach available through Go's gob package.
Go has a package called gob that can be used to encode and decode data in binary format. To communicate between two applications written in Go, you can use gob to encode data into binary and then decode it back out of binary.
This approach has advantages and drawbacks compared to standard formats such as JSON, XML, and protobuffer.
Data is converted to binary, so it's smaller than XML and JSON. When transferring data, the smaller the data, the faster the transfer.
Support for complex Go structures such as pointers, maps, structs, and so on.
High performance and simple to implement.
It can only be used within Go, so if your applications are written in different languages, you can't use Gob as a data-transfer format between them.
No validation is performed on the data while decoding it. So gob isn't suitable for untrusted or unvalidated data.
Using the gob package, we can convert data to binary as follows:
Using the NewEncoder function, we specify where the data should be written, and finally, using the Encode function, we convert the data into binary format. In the example above, the data is written in binary form into a file named contacts.gob.
Also, to decode data from gob format back into Go data, we can do the following:
In the example above, we decode the data that was stored in binary format in the contacts.gob file, put it into contactsToLoad, and finally print it to standard output.
This post's content was written based on the following resources:
You can find the code related to this post in our GitHub repository.
Previous part: JSON | Golang basics