Documentation
¶
Overview ¶
Package encoding 提供统一的编解码器接口和多种编码格式支持。 支持 JSON、Protobuf、TOML、XML、YAML、MsgPack 等格式。
使用示例:
// 注册自定义编解码器
encoding.Register(myCodec)
// 获取并使用编解码器
codec := encoding.Invoke("json")
data, err := codec.Marshal(obj)
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Codec ¶
type Codec interface {
// Name 返回编解码器的名称,用于注册和查找
Name() string
// Marshal 将对象编码为字节数组
Marshal(v any) ([]byte, error)
// Unmarshal 将字节数组解码为对象
Unmarshal(data []byte, v any) error
}
Codec 编解码器接口,定义了数据序列化和反序列化的标准方法。 实现该接口的类型可以注册到编码包中,供框架统一使用。
func InvokeWithError ¶ added in v1.0.9
InvokeWithError 根据名称获取已注册的编解码器,返回错误而非 panic。 如果编解码器不存在,返回 CodecNotRegistered 错误。
Click to show internal directories.
Click to hide internal directories.