Golang encoding: Difference between revisions

From wikinotes
No edit summary
Line 25: Line 25:
|-
|-
| [[golang encoding/xml]]
| [[golang encoding/xml]]
|-
| [[golang encoding/json]]
|-
|-
|}
|}
</blockquote><!-- Builtin -->
</blockquote><!-- Builtin -->
</blockquote><!-- Libraries -->
</blockquote><!-- Libraries -->

Revision as of 02:56, 25 June 2022

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
golang encoding/json