Documentation
¶
Overview ¶
Package encoding provides a registry for named codecs.
Codec packages such as encoding/json, encoding/yaml, encoding/toml, and encoding/proto register themselves from init. Consumers blank import the codec packages they need, then call GetCodec by content subtype.
Index ¶
Examples ¶
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.
Types ¶
type Codec ¶
type Codec interface {
// Marshal returns the wire format of v.
Marshal(v interface{}) ([]byte, error)
// Unmarshal parses the wire format into v.
Unmarshal(data []byte, v interface{}) 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.
The content-subtype is expected to be lowercase.
Example ¶
package main
import (
"fmt"
"github.com/go-jimu/components/encoding"
_ "github.com/go-jimu/components/encoding/json"
)
type examplePayload struct {
Name string `json:"name"`
}
func main() {
codec := encoding.GetCodec("json")
if codec == nil {
panic("json codec is not registered")
}
data, err := codec.Marshal(examplePayload{Name: "agent"})
if err != nil {
panic(err)
}
fmt.Println(codec.Name(), string(data))
}
Output: json {"name":"agent"}
Directories
¶
| Path | Synopsis |
|---|---|
|
Package json registers the JSON codec with the encoding registry.
|
Package json registers the JSON codec with the encoding registry. |
|
Package proto defines the protobuf codec.
|
Package proto defines the protobuf codec. |
|
Package toml registers the TOML codec with the encoding registry.
|
Package toml registers the TOML codec with the encoding registry. |
|
Package yaml registers the YAML codec with the encoding registry.
|
Package yaml registers the YAML codec with the encoding registry. |