The Go encoding package defines a set of standard interfaces for converting data between Go data structures and byte representations (serialization and deserialization). While encoding itself consists mostly of interface definitions, its specialized subpackages implement protocols for binary, text, and web formats.
Key Subpackages & Formats:
- Structured Data:
encoding/json(JSON parsing/generation),encoding/xml(XML encoding), andencoding/csv(comma-separated value reader/writer). - Binary & Byte Formats:
encoding/binary(translates numbers to/from fixed-length byte sequences using big/little-endian byte orders),encoding/gob(Go-specific binary format for RPCs and inter-process communication). - Text Encodings:
encoding/base64,encoding/base32, andencoding/hex(encodes binary data into human-readable ASCII text formats).
Core Interfaces:
- **
BinaryMarshaler/BinaryUnmarshaler**: Implemented by types that can serialize/deserialize themselves to/from raw binary byte slices. - **
TextMarshaler/TextUnmarshaler**: Implemented by types that can encode/decode themselves into UTF-8 text representations.
By implementing these interfaces on custom structs, types automatically become compatible with standard tools like json.Marshal, base64.StdEncoding, or SQL database drivers.