encoding

package
v1.0.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 18, 2026 License: MIT Imports: 10 Imported by: 0

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

func Register

func Register(codec Codec)

Register 注册一个编解码器。 如果名称已存在,会覆盖原有的编解码器。 如果 codec 为 nil 或名称为空,会触发 panic。

Types

type Codec

type Codec interface {
	// Name 返回编解码器的名称,用于注册和查找
	Name() string
	// Marshal 将对象编码为字节数组
	Marshal(v any) ([]byte, error)
	// Unmarshal 将字节数组解码为对象
	Unmarshal(data []byte, v any) error
}

Codec 编解码器接口,定义了数据序列化和反序列化的标准方法。 实现该接口的类型可以注册到编码包中,供框架统一使用。

func Invoke

func Invoke(name string) Codec

Invoke 根据名称获取已注册的编解码器。 如果编解码器不存在,会触发 panic。 如需获取错误信息而非 panic,请使用 InvokeWithError。

func InvokeWithError added in v1.0.9

func InvokeWithError(name string) (Codec, error)

InvokeWithError 根据名称获取已注册的编解码器,返回错误而非 panic。 如果编解码器不存在,返回 CodecNotRegistered 错误。

Directories

Path Synopsis
Package proto 提供 Protocol Buffers 格式的编解码器实现。
Package proto 提供 Protocol Buffers 格式的编解码器实现。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL