encoding

package module
v1.5.9 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: MIT Imports: 11 Imported by: 0

README

关于本项目

本项目完全由 AI 维护。代码源自 github.com/ssgo/u 的重构。

@go/encoding

@go/encoding 是一个为“极简、安全、无摩擦”业务开发设计的编解码工具库。它统一了二进制数据与文本处理的 API 语义,通过与 go/cast 结合,极大降低了业务逻辑中的错误处理心智负担。

🎯 设计哲学

  • API 原生直觉:二进制操作基于 []byte,文本表现类操作(如 HTML/URL)基于 string,与 Go 原生习惯保持高度一致。
  • 消除摩擦 (Frictionless):废除 Must 前缀函数,推荐配合 cast.As 使用以实现更优雅的静默处理,降低业务代码中无效的错误检查噪声。
  • 极致纯粹:废除所有冗余的封装,强制数据链路层以 []byte 形式流转,确保底层安全性与性能。

🛠 API Reference

基础编解码 (Hex/Base64)
  • func Hex(data []byte) []byte / func HexToString(data []byte) string
  • func UnHex(data []byte) ([]byte, error) / func UnHexFromString(data string) ([]byte, error)
  • func Base64(data []byte) []byte / func Base64ToString(data []byte) string
  • func Base64Raw(data []byte) []byte / func Base64RawToString(data []byte) string (无填充版本)
  • func UnBase64(data []byte) ([]byte, error) / func UnBase64FromString(data string) ([]byte, error) (智能兼容填充与无填充)
  • func UrlBase64(data []byte) []byte / func UrlBase64ToString(data []byte) string
  • func UrlBase64Raw(data []byte) []byte / func UrlBase64RawToString(data []byte) string (无填充版本)
  • func UnUrlBase64(data []byte) ([]byte, error) / func UnUrlBase64FromString(data string) ([]byte, error) (智能兼容填充与无填充)
Web 编码 (URL/HTML)
  • func UrlEncode(data []byte) string
  • func UnUrlEncode(data string) ([]byte, error)
  • func HtmlEscape(data []byte) string
  • func HtmlUnescape(data string) string
  • func Utf8Valid(data []byte) bool
签名工具 (Signature Utils)
  • func SortJoin(v any, separator, connector string, urlEncode bool) string 将 Map 或 Struct 转换为排序并拼接后的字符串。常用于生成签名原串。支持自动 URL 编码。
整数与自定义进制 (IntEncoder)
  • func EncodeInt(u uint64) []byte
  • func AppendInt(buf []byte, u uint64) []byte
  • func DecodeInt(buf []byte) uint64
  • func FillInt(buf []byte, length int) []byte
  • func ExchangeInt(buf []byte) []byte
  • func HashInt(buf []byte) []byte
  • func FoldInt(buf []byte, length int) []byte

📦 安装

go get github.com/api-go/encoding

💡 示例 (配合 cast.As 消除摩擦)

import (
    "github.com/api-go/encoding"
    "github.com/api-go/cast"
)

// 配合 cast.As 替代原有的 MustUnHex 系列
data := cast.As(encoding.UnHexFromString("68656c6c6f"))

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultIntEncoder, _ = NewIntEncoder("9ukH1grX75TQS6LzpFAjIivsdZoO0mc8NBwnyYDhtMWEC2V3KaGxfJRPqe4lbU", 62)

默认编码器实例

View Source
var OrderedIntEncoder, _ = NewIntEncoder("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", 62)

Functions

func AppendInt

func AppendInt(buf []byte, u uint64) []byte

AppendInt 使用默认编码器将整数追加到已有字节切片中

func Base64

func Base64(data any) string

Base64 将数据转换为 Base64 编码的字符串

func Base64Raw

func Base64Raw(data any) string

Base64Raw 将数据转换为无填充的 Base64 编码的字符串

func DecodeInt

func DecodeInt(data any) uint64

DecodeInt 使用默认编码器从数据解码为整数

func EncodeInt

func EncodeInt(u uint64) []byte

EncodeInt 使用默认编码器将整数转换为字节切片

func ExchangeInt

func ExchangeInt(data any) []byte

ExchangeInt 使用默认编码器对数据进行位置交替重排

func FillInt

func FillInt(data any, length int) []byte

FillInt 使用默认编码器填充数据至指定长度

func HTMLEscape

func HTMLEscape(data any) string

HTMLEscape 对数据进行 HTML 转义

func HTMLUnescape

func HTMLUnescape(data any) string

HTMLUnescape 对 HTML 字符串进行反转义

func HashInt

func HashInt(buf []byte) []byte

HashInt 对数据进行渐进式置换混淆

func Hex

func Hex(data any) string

Hex 将数据转换为 Hex 编码的字符串

func SortJoin

func SortJoin(v any, separator, connector string, urlEncode bool) string

SortJoin 将 Map 或 Struct 转换为排序并拼接后的字符串 (常用于签名)

func URLBase64

func URLBase64(data any) string

URLBase64 将数据转换为 URL 安全的 Base64 编码的字符串

func URLBase64Raw

func URLBase64Raw(data any) string

URLBase64Raw 将数据转换为 URL 安全且无填充的 Base64 编码的字符串

func URLEncode

func URLEncode(data any) string

URLEncode 对数据进行 URL 编码

func UTF8Valid

func UTF8Valid(data any) bool

UTF8Valid 检查数据是否为有效的 UTF-8 编码

func UnBase64

func UnBase64(data any) ([]byte, error)

UnBase64 将 Base64 编码的数据解码为字节切片(自动兼容有无填充)

func UnHex

func UnHex(data any) ([]byte, error)

UnHex 将 Hex 编码的数据解码为字节切片

func UnURLBase64

func UnURLBase64(data any) ([]byte, error)

UnURLBase64 将 URL 安全的 Base64 编码的数据解码为字节切片(自动兼容有无填充)

func UnURLEncode

func UnURLEncode(data any) ([]byte, error)

UnURLEncode 对字符串进行 URL 解码

Types

type IntEncoder

type IntEncoder struct {
	// contains filtered or unexported fields
}

IntEncoder 提供整数与字节切片之间的自定义进制转换

func NewIntEncoder

func NewIntEncoder(digits string, radix uint8) (*IntEncoder, error)

NewIntEncoder 创建一个新的整数编码器

func (*IntEncoder) AppendInt

func (enc *IntEncoder) AppendInt(buf []byte, u uint64) []byte

AppendInt 将整数追加到已有字节切片中

func (*IntEncoder) DecodeInt

func (enc *IntEncoder) DecodeInt(data any) uint64

DecodeInt 从数据解码为整数

func (*IntEncoder) EncodeInt

func (enc *IntEncoder) EncodeInt(u uint64) []byte

EncodeInt 将整数转换为字节切片

func (*IntEncoder) ExchangeInt

func (enc *IntEncoder) ExchangeInt(data any) []byte

ExchangeInt 对数据进行位置交替重排,就地修改并返回

func (*IntEncoder) FillInt

func (enc *IntEncoder) FillInt(data any, length int) []byte

FillInt 使用随机字符序列填充数据至指定长度,高并发下具备零锁极速随机性能,就地修改并返回

func (*IntEncoder) FoldInt

func (enc *IntEncoder) FoldInt(buf []byte, length int) []byte

FoldInt 使用编码器的字符集模加法,将超出 length 的尾部字节折叠到前 length 字节中,就地修改并截断返回

func (*IntEncoder) HashInt

func (enc *IntEncoder) HashInt(buf []byte) []byte

HashInt 双向非线性渐进式置换混淆,原地修改并返回,所有值保持在 radix 字符集内

Jump to

Keyboard shortcuts

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