Golang encoding

From wikinotes
Revision as of 02:56, 25 June 2022 by Will (talk | contribs)

Golang's encoding module defines the interfaces for data serialization.

Basics

Each method of encoding implements at least one of these interfaces

type BinaryMarshaler   interface { MarshalBinary() (data []byte, err error) }
type BinaryUnmarshaler interface { UnmarshalBinary(data []byte) error }
type TextMarshaler     interface { MarshallText(text []byte) error }
type TextUnmarshaler   interface { UnmarshalText(text []byte) error }

Rather than a top-down approach, you define types that can be serialized/deserialized.
When deserializing, you cat the type, and deserialization is automatic.

Libraries

Builtin

golang encoding/xml