Documentation
¶
Overview ¶
Package encoding defines the Codec contract — Marshal, Unmarshal, and a stable Name — and a process-wide registry that resolves a codec by the content subtype it serves.
RegisterCodec installs a codec, normally from an init function in the codec's own package; GetCodec resolves one by name. The subpackages json, xml, yaml, form, proto, and protojson each provide a codec and register it on import, so linking a codec in is a blank import away. Additional codecs live under contrib/encoding/ as separate modules.
The registry serves callers for whom a codec name arrives as data: a config source's format tag, an HTTP Content-Type subtype. Registration is additive and names are stable, so what a name resolves to never depends on initialization order.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RegisterCodec ¶
func RegisterCodec(codec Codec)
RegisterCodec registers the provided Codec for use with all Transport clients and servers. It is safe to call concurrently with itself and with GetCodec; registering a Codec whose name is already registered replaces the earlier one.
Types ¶
type Codec ¶
type Codec interface {
// Marshal returns the wire format of v.
Marshal(v any) ([]byte, error)
// Unmarshal parses the wire format into v.
Unmarshal(data []byte, v any) error
// Name returns the name of the Codec implementation. The returned string
// will be used as part of content type in transmission. The result must be
// static; the result cannot change between calls.
Name() string
}
Codec defines the interface Transport uses to encode and decode messages. Note that implementations of this interface must be thread safe; a Codec's methods can be called from concurrent goroutines.
func GetCodec ¶
GetCodec gets a registered Codec by content-subtype, or nil if no Codec is registered for the content-subtype. It is safe to call concurrently with RegisterCodec.
The content-subtype is expected to be lowercase.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package form provides the URL-encoded form encoding.Codec, handling both plain Go structs (via go-playground/form) and Protobuf messages (field-name aware, honoring json_name).
|
Package form provides the URL-encoded form encoding.Codec, handling both plain Go structs (via go-playground/form) and Protobuf messages (field-name aware, honoring json_name). |
|
Package json provides the JSON encoding.Codec, backed by encoding/json.
|
Package json provides the JSON encoding.Codec, backed by encoding/json. |
|
Package proto defines the protobuf codec.
|
Package proto defines the protobuf codec. |
|
Package protojson provides the ProtoJSON encoding.Codec for Protobuf messages, backed by google.golang.org/protobuf/encoding/protojson.
|
Package protojson provides the ProtoJSON encoding.Codec for Protobuf messages, backed by google.golang.org/protobuf/encoding/protojson. |
|
Package xml provides the XML encoding.Codec, backed by encoding/xml.
|
Package xml provides the XML encoding.Codec, backed by encoding/xml. |
|
Package yaml provides the YAML encoding.Codec.
|
Package yaml provides the YAML encoding.Codec. |