|
|
Line 1: |
Line 1: |
| 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(text []byte) error }
| |
| </syntaxhighlight>
| |
|
| |
| Rather than a top-down approach, you define types that can be serialized/deserialized.<br>
| |
| When deserializing, you cat the type, and deserialization is automatic.
| |
| <syntaxhighlight lang="go">
| |
|
| |
| </syntaxhighlight>
| |
| </blockquote><!-- Basics -->
| |
|
| |
| = Libraries =
| |
| <blockquote>
| |
| == Builtin == | | == Builtin == |
| <blockquote> | | <blockquote> |
Line 32: |
Line 11: |
| |} | | |} |
| </blockquote><!-- Builtin --> | | </blockquote><!-- Builtin --> |
| </blockquote><!-- Libraries -->
| |