encoding

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: May 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(data []byte, key []byte) []byte

📦 安装

go get apigo.cc/go/encoding

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

import (
    "apigo.cc/go/encoding"
    "apigo.cc/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 []byte) []byte

Base64 将数据转换为 Base64 编码的字节切片

func Base64Raw added in v1.1.0

func Base64Raw(data []byte) []byte

Base64Raw 将数据转换为无填充的 Base64 编码的字节切片

func Base64RawToString added in v1.1.0

func Base64RawToString(data []byte) string

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

func Base64ToString

func Base64ToString(data []byte) string

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

func DecodeInt

func DecodeInt(buf []byte) uint64

DecodeInt 使用默认编码器从字节切片解码为整数

func EncodeInt

func EncodeInt(u uint64) []byte

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

func ExchangeInt

func ExchangeInt(buf []byte) []byte

func FillInt added in v1.0.4

func FillInt(buf []byte, length int) []byte

func HashInt

func HashInt(data []byte, key []byte) []byte

func Hex

func Hex(data []byte) []byte

Hex 将数据转换为 Hex 编码的字节切片

func HexToString

func HexToString(data []byte) string

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

func HtmlEscape

func HtmlEscape(data []byte) string

HtmlEscape 对数据进行 HTML 转义

func HtmlUnescape added in v1.0.6

func HtmlUnescape(data string) string

HtmlUnescape 对 HTML 字符串进行反转义

func SortJoin added in v1.1.1

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

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

func UnBase64

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

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

func UnBase64FromString added in v1.0.6

func UnBase64FromString(data string) ([]byte, error)

UnBase64FromString 将 Base64 编码的字符串解码(自动兼容有无填充)

func UnHex

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

UnHex 将 Hex 编码的字节切片解码

func UnHexFromString added in v1.0.6

func UnHexFromString(data string) ([]byte, error)

UnHexFromString 将 Hex 编码的字符串解码

func UnUrlBase64

func UnUrlBase64(data []byte) ([]byte, error)

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

func UnUrlBase64FromString added in v1.0.6

func UnUrlBase64FromString(data string) ([]byte, error)

UnUrlBase64FromString 将 URL 安全的 Base64 编码的字符串解码(自动兼容有无填充)

func UnUrlEncode added in v1.0.6

func UnUrlEncode(data string) ([]byte, error)

UnUrlEncode 对字符串进行 URL 解码

func UrlBase64

func UrlBase64(data []byte) []byte

UrlBase64 将数据转换为 URL 安全的 Base64 编码的字节切片

func UrlBase64Raw added in v1.1.0

func UrlBase64Raw(data []byte) []byte

UrlBase64Raw 将数据转换为 URL 安全且无填充的 Base64 编码的字节切片

func UrlBase64RawToString added in v1.1.0

func UrlBase64RawToString(data []byte) string

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

func UrlBase64ToString

func UrlBase64ToString(data []byte) string

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

func UrlEncode

func UrlEncode(data []byte) string

UrlEncode 对数据进行 URL 编码

func Utf8Valid

func Utf8Valid(data []byte) bool

Utf8Valid 检查字节切片是否为有效的 UTF-8 编码

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(buf []byte) uint64

DecodeInt 从字节切片解码为整数

func (*IntEncoder) EncodeInt

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

EncodeInt 将整数转换为字节切片

func (*IntEncoder) ExchangeInt

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

ExchangeInt 对字节切片进行位置交替重排

func (*IntEncoder) FillInt

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

FillInt 使用循环字符序列填充字节切片至指定长度

func (*IntEncoder) HashInt

func (enc *IntEncoder) HashInt(data []byte, key []byte) []byte

HashInt 对字节切片进行 HMAC-SHA512 哈希

Jump to

Keyboard shortcuts

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