utils

package
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2026 License: Apache-2.0 Imports: 34 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

本文件提供基于 SHA-256 密钥和 AES-GCM 的字符串加解密能力。

本文件提供结构字段自动映射、自定义映射和字段索引缓存。

本文件提供保序、有界且可取消的批量任务执行能力。

本文件提供基础类型转换、数值相加和安全的字节字符串转换。

本文件定义兼容 shopspring/decimal 的具名 Decimal 类型。

本文件提供运行路径、文件和目录操作的兼容辅助函数。

本文件提供兼容的 MD5、SHA-256、用户标识和组合哈希函数。

本文件提供保留旧错误语义的 JSON 序列化辅助函数。

本文件提供 PBKDF2 密钥、随机盐值和 JWT key 派生能力。

本文件仅为兼容保留旧 DES、3DES 和 padding API,新代码不应继续使用。

本文件提供本机 IP、可信代理客户端地址和端口探测能力。

本文件提供整数、浮点数和可空数字文本校验。

本文件保留旧的进程级伪随机数兼容入口。

本文件提供反射类型识别、字段访问、结构遍历和实例创建能力。

本文件提供测试运行环境判断。

本文件保留 Snowflake worker 的兼容构造入口。

本文件提供 Unicode 安全的首字符转换和 rune 辅助函数。

本文件保留旧的毫秒时间戳文本转换入口。

本文件提供邮箱和手机号文本的兼容校验函数。

Index

Constants

This section is empty.

Variables

View Source
var TESTPATH string

TESTPATH 指定测试运行时使用的根路径。

Functions

func Add

func Add(v1, v2 reflect.Value) reflect.Value

Add 对两个同类型数值或 Decimal 执行加法。

func AnyToTypeData

func AnyToTypeData(value interface{}, src reflect.Type) (interface{}, error)

AnyToTypeData 将值转换为指定基础类型。

func ArrayEach

func ArrayEach(items interface{}, f func(item interface{}))

ArrayEach 逐个回调数组或切片元素。

func AutoMapConvert

func AutoMapConvert(source, target interface{}, getmap func(*AutoMapArge) *AutoMapHander) interface{}

AutoMapConvert 将同名字段从源对象映射到目标对象,并允许调用方覆盖映射结果。

func Bytes2String

func Bytes2String(b []byte) string

Bytes2String 返回字节内容的独立字符串副本。

func ClientPublicIP

func ClientPublicIP(r *http.Request, trustedProxies ...string) string

ClientPublicIP 按可信代理链解析请求的客户端 IP。

func ConvertToString

func ConvertToString(value interface{}) string

ConvertToString 将支持的基础类型转换为字符串。

func CreateDir

func CreateDir(name string) (string, error)

CreateDir 在当前运行路径创建指定名称目录,当测试时,目录的创建路径为TESTPATH

func Decrptog3DES

func Decrptog3DES(src, key []byte) []byte

Decrptog3DES 使用旧 key 派生 IV 的 3DES-CBC 算法解密数据。

func DecrptogDES

func DecrptogDES(src, key []byte) []byte

DecrptogDES 使用旧固定 IV 的 DES-CBC 算法解密数据。

func DecryptAES

func DecryptAES(ciphertext, key string) (string, error)

DecryptAES 解密 EncryptAES 生成的 URL-safe Base64 文本。

func DeepFor

func DeepFor(stype reflect.Type, forfunc func(field, parent reflect.StructField, kind TypeKind))

DeepFor 深度枚举指定反射类型的导出字段。

func DeepForItem

func DeepForItem(item interface{}, forfunc func(field, parent reflect.StructField, kind TypeKind))

DeepForItem 深度枚举对象的导出字段。

func DeleteFile

func DeleteFile(file string) error

DeleteFile 删除指定文件。

func DeriveJWTKey

func DeriveJWTKey(password, userID string) string

DeriveJWTKey 使用用户标识参与盐值构造,派生 URL-safe JWT key。

func DeriveKey

func DeriveKey(password, salt string, keyLen int) []byte

DeriveKey 使用旧版 10,000 次 PBKDF2 迭代派生兼容密钥。

func DeriveKeySecure

func DeriveKeySecure(password string, salt []byte, keyLen int) []byte

DeriveKeySecure 使用 100,000 次 PBKDF2 迭代派生密钥。

func DeriveKeyWithSalt

func DeriveKeyWithSalt(password string, keyLen int) ([]byte, []byte, error)

DeriveKeyWithSalt 生成 16 字节随机盐值并派生密钥。

func EncryptAES

func EncryptAES(plaintext, key string) (string, error)

EncryptAES 使用 key 的 SHA-256 摘要通过 AES-GCM 加密文本。

func Encyptog3DES

func Encyptog3DES(src, key []byte) []byte

Encyptog3DES 使用旧 key 派生 IV 的 3DES-CBC 算法加密数据。

func EncyptogDES

func EncyptogDES(src, key []byte) []byte

EncyptogDES 使用旧固定 IV 的 DES-CBC 算法加密数据。

func FirstLower

func FirstLower(s string) string

FirstLower 将字符串的第一个 Unicode 字符转换为小写。

func FirstUpper

func FirstUpper(s string) string

FirstUpper 将字符串的第一个 Unicode 字符转换为大写。

func ForEach

func ForEach(item interface{}, fn func(name string, value interface{}))

ForEach 逐个回调对象的导出字段名和值。

func ForItem

func ForItem(item interface{}, value func(name string) interface{})

ForItem 使用回调返回值更新对象的可设置字段。

func GenerateSalt

func GenerateSalt(length int) ([]byte, error)

GenerateSalt 使用密码学随机源生成指定长度的盐值。

func GetElem

func GetElem(typ reflect.Type) reflect.Type

GetElem 返回指针元素类型,非指针类型原样返回。

func GetLocalIP

func GetLocalIP() string

GetLocalIP 返回枚举到的第一个非回环 IPv4 地址。

func GetLocalIPs

func GetLocalIPs() ([]string, error)

GetLocalIPs 返回所有非回环 IPv4 地址。

func GetOutBoundIP

func GetOutBoundIP() (ip string, err error)

GetOutBoundIP 通过 UDP 路由选择取得默认出口 IP。

func GetPackageName

func GetPackageName(target interface{}) string

GetPackageName 返回对象元素类型的完整包路径。

func GetParentType

func GetParentType(target interface{}) interface{}

GetParentType 返回旧继承结构中与当前类型同名的匿名结构字段类型。

func GetPropertyType

func GetPropertyType(target interface{}, name string) *reflect.StructField

GetPropertyType 按 Go 字段名或 JSON tag 查找结构字段。

func GetPropertyTypeByElemName

func GetPropertyTypeByElemName(target interface{}, elemTypeName string) *reflect.StructField

GetPropertyTypeByElemName 通过字段元素类型名查找字段 例如字段 PriceSources []*IndexPriceSourceModel,elemTypeName="IndexPriceSourceModel" 可以找到该字段

func GetPropertyValue

func GetPropertyValue(target interface{}, name string) interface{}

GetPropertyValue 返回结构体指定字段的值,字段缺失时返回空字符串。

func GetRandNum

func GetRandNum(n int) int

GetRandNum 返回区间 [0,n) 内的伪随机整数。

func GetTypeAndValue

func GetTypeAndValue(target interface{}) (reflect.Type, reflect.Value)

GetTypeAndValue 返回对象解引用一层后的类型和值。

func GetTypeName

func GetTypeName(item interface{}) string

GetTypeName 返回对象元素类型的名称。

func Getpath

func Getpath() string

Getpath 获取当前路径,当为测试运行时,使用TESTPATH

func HasLocalIP

func HasLocalIP(ip net.IP) bool

HasLocalIP 检测 IP 地址是否是内网地址 通过直接对比ip段范围效率更高

func HasLocalIPAddr

func HasLocalIPAddr(ip string) bool

HasLocalIPAddr 检测 IP 地址是否是内网地址

func HasProperty

func HasProperty(target interface{}, name string) bool

HasProperty 报告结构体是否包含指定 Go 字段。

func HashCode64

func HashCode64(s string) uint64

HashCode64 返回输入 SHA-256 摘要前 8 字节对应的无符号整数。

func HashCodeHex

func HashCodeHex(s string) string

HashCodeHex 是 SecureHash 的兼容别名。

func HashCodes

func HashCodes(strings ...string) string

HashCodes 返回多个字符串按旧分隔格式组合后的 32 字符哈希。

func IsArray

func IsArray(items interface{}) bool

IsArray 报告对象或其指针元素是否为数组或切片。

func IsDir

func IsDir(path string) bool

IsDir 判断是否为目录

func IsEmail

func IsEmail(device string) bool

IsEmail 报告文本能否被标准库解析为邮件地址。

func IsEqual

func IsEqual(v1, v2 interface{}) bool

IsEqual 报告两个同类型值是否深度相等。

func IsExista

func IsExista(file string) bool

IsExista 判断文件是否存在

func IsFile

func IsFile(path string) bool

IsFile 判断是否为文件

func IsFloat

func IsFloat(s string) bool

IsFloat 报告文本能否解析为浮点数。 注意:如果字符串是整数形式的,也会返回true 例如 "123" 会被认为是浮点数

func IsInteger

func IsInteger(s string) bool

IsInteger 报告文本能否解析为整数。

func IsMobile

func IsMobile(device string) bool

IsMobile 按旧的“区号 空格 号码”格式判断手机号文本。

func IsNumber

func IsNumber(s string) bool

IsNumber 报告文本能否解析为整数或浮点数。

func IsNumberOrNil

func IsNumberOrNil(s string) bool

IsNumberOrNil 报告文本为空或能否解析为数字。

func IsNumberOrNilFloat

func IsNumberOrNilFloat(s string) bool

IsNumberOrNilFloat 报告文本为空或能否解析为浮点数。

func IsNumberOrNilInt

func IsNumberOrNilInt(s string) bool

IsNumberOrNilInt 报告文本为空或能否解析为整数。

func IsPtr

func IsPtr(instance interface{}) bool

IsPtr 报告对象的直接类型是否为指针。

func IsTest

func IsTest() bool

IsTest 报告当前进程是否注册了 Go test 或 testify 的测试参数。

func IsTypeKind

func IsTypeKind(typ reflect.Type, kind TypeKind) bool

IsTypeKind 报告反射类型是否属于指定归一类别。

func Md5

func Md5(strs ...string) string

Md5 返回所有参数直接拼接后的 MD5 十六进制摘要。

func MediumHash

func MediumHash(s string) string

MediumHash 返回输入的 32 字符 SHA-256 截断哈希。

func NewAlgorithmSnowFlake

func NewAlgorithmSnowFlake(machineId uint, dataCenterId uint) idgen.ISnowWorker

NewAlgorithmSnowFlake 按旧十进制拼接规则组合 machineId 和 dataCenterId。

func NewArrayItem

func NewArrayItem(items interface{}) interface{}

NewArrayItem 创建数组或切片元素类型的零值指针实例。

func NewInterface

func NewInterface(obj interface{}) interface{}

NewInterface 按对象的元素类型创建一个零值指针实例。

func NewInterfaceByType

func NewInterfaceByType(typ reflect.Type) interface{}

NewInterfaceByType 按类型创建一个零值实例(指针)。 说明:此前基于 sync.Pool 的对象池因缺少归还路径(RecycleObject 从未被调用)而从未生效, 且路由层已有独立对象池,故此处直接用 reflect.New 创建全新零值对象,语义等价且更简洁。

func PaddingText

func PaddingText(str []byte, blockSize int) []byte

PaddingText 按 PKCS#7 风格填充数据。

func PrintObj

func PrintObj(o interface{}) string

PrintObj 将对象序列化为 JSON;序列化失败时沿用旧行为返回空字符串。

func ReadFile

func ReadFile(file string) (string, error)

ReadFile 读取文件全部内容并返回字符串。

func RecycleObject

func RecycleObject(obj interface{})

RecycleObject 保留旧对象池 API 的源码兼容性。

NewInterfaceByType 现在直接创建全新零值对象,路由层对象复用由独立池负责; 因此这里不再执行实际归还动作。

func ScanPort

func ScanPort(protocol string, hostname string, port int) bool

ScanPort 在三秒超时内探测指定协议和端口能否建立连接。

func SecureHash

func SecureHash(s string) string

SecureHash 返回输入的完整 SHA-256 十六进制哈希。

func SetPropertyValue

func SetPropertyValue(sender interface{}, name string, value interface{}) error

SetPropertyValue 按不区分大小写的字段名转换并设置值。

func ShortHash

func ShortHash(s string) string

ShortHash 返回输入的 16 字符 SHA-256 截断哈希。

func StopMemoryMonitor

func StopMemoryMonitor()

StopMemoryMonitor 为旧反射缓存监控的兼容入口。 Deprecated: utils 不再启动包级内存监控,调用该函数不会执行操作。

func String2Bytes

func String2Bytes(s string) []byte

String2Bytes 返回字符串内容的独立字节副本。

func ToTime

func ToTime(s string) time.Time

ToTime 将毫秒时间戳文本转换为 time.Time,解析失败时沿用旧的零时间戳行为。

func TrimFirstRune

func TrimFirstRune(s string) string

TrimFirstRune 删除字符串的第一个 Unicode 字符。

func UnPaddingText

func UnPaddingText(str []byte) []byte

UnPaddingText 按旧语义移除末尾 padding。

func UserIDHash

func UserIDHash(externalID string) string

UserIDHash 返回外部用户标识的 32 字符哈希。

func UserIDUUID

func UserIDUUID(externalID string) string

UserIDUUID 返回由外部用户标识稳定生成的 UUID。

Types

type AutoMapArge

type AutoMapArge struct {
	Field       string
	Value       string
	FieldValue  interface{}
	SourceName  string
	TargetName  string
	SourceType  reflect.Type
	TargetType  reflect.Type
	FieldType   reflect.StructField
	SourceValue interface{}
	TargetValue interface{}
}

AutoMapArge 描述自定义字段映射时的源字段和目标对象上下文。

type AutoMapHander

type AutoMapHander struct {
	TargetField string
	TargetValue interface{}
	TargetItems map[string]interface{}
}

AutoMapHander 描述自定义映射返回的单字段或多字段赋值。

type ConcurrencyTasks

type ConcurrencyTasks[T interface{}] struct {
	Ctx         context.Context
	Params      []T
	Results     []interface{}
	Func        func(param T) (interface{}, error)
	Concurrency int
}

ConcurrencyTasks 按 Params 顺序保存并发任务的结果或错误。

Func 已经开始执行后,取消 Ctx 不会强制中断它;Func 需要自行响应业务上下文。

func (*ConcurrencyTasks[T]) GetErr

func (t *ConcurrencyTasks[T]) GetErr() error

GetErr 返回 Results 中第一个错误;没有错误时返回 nil。

func (*ConcurrencyTasks[T]) Run

func (t *ConcurrencyTasks[T]) Run()

Run 使用固定数量的 worker 执行任务,并保持 Results 与 Params 的索引一致。

func (*ConcurrencyTasks[T]) Successes

func (t *ConcurrencyTasks[T]) Successes() int

Successes 返回没有保存 error 的结果数量。

type Decimal

type Decimal decimal.Decimal

Decimal 是 decimal.Decimal 的兼容具名类型。

func (Decimal) Equals

func (d Decimal) Equals(other Decimal) bool

Equals 报告两个 Decimal 是否相等。

func (Decimal) Less

func (d Decimal) Less(other Decimal) bool

Less 报告当前 Decimal 是否小于另一个值。

type TypeKind

type TypeKind int

TypeKind 表示工具包归一后的反射类型类别。

const (
	Base    TypeKind = iota // Base 表示数字、布尔、字符串和复数等基础类型。
	Array                   // Array 表示数组、切片、映射和通道等集合类型。
	Ptr                     // Ptr 表示指针类型。
	Func                    // Func 表示函数或接口类型。
	Struct                  // Struct 表示结构体类型。
	Invalid                 // Invalid 表示未归类或 nil 类型。
)

func GetTypeKind

func GetTypeKind(typ reflect.Type) TypeKind

GetTypeKind 将 reflect.Type 归一为 TypeKind。

Jump to

Keyboard shortcuts

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