Golang encoding

From wikinotes
Revision as of 02:55, 25 June 2022 by Will (talk | contribs) (Created page with "Golang's <code>encoding</code> module defines the interfaces for data serialization. = Basics = <blockquote> Each method of encoding implements at least one of these interfaces <syntaxhighlight lang="go"> 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(t...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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.