util

package
v0.0.0-...-80248e1 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2025 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AdjustBufferSize

func AdjustBufferSize(buf []byte, usedSize, minSpace, initSize int) []byte

AdjustBufferSize 调整缓冲区大小

扩容策略:
	如果剩余空间小于 minSpace, 则扩容
缩容策略:
	如果使用的大小, 小于缓冲区大小的1/4, 并且initSize小于缓冲区大小, 则缩容为缓冲区大小的一半
参数:
	buf: 原缓冲区
	usedSize: 使用的大小
	minSpace: 最小剩余空间要求(通常为1024)
	initSize: 初始缓冲区大小(通常为2048)
返回:
	[]byte: 调整后的缓冲区

func Float32Equal

func Float32Equal(a, b float32) bool

Float32Equal 判断两个 float32 是否相等

func Float32Less

func Float32Less(a, b float32) bool

func GenToken

func GenToken(prefix string) string

GenToken 生成token

参数:
	prefix: token前缀,可为空
返回值:
	token: 安全的随机字符串

func GetFuncName

func GetFuncName(i any, seps ...rune) string

GetFuncName 获取函数名称

func GetGoroutineID

func GetGoroutineID() uint64

GetGoroutineID 获取当前协程的ID

func HASH32

func HASH32(data []byte) uint32

HASH32 32位哈希

func HASH64

func HASH64(data []byte) uint64

HASH64 64位哈希

func HexStringToUint32

func HexStringToUint32(hexStr string) (uint32, error)

HexStringToUint32 将十六进制字符串转换为 uint32

支持 "0x" 和 "0X" 前缀

func If

func If[T any](condition bool, trueFn, falseFn func() T) T

If 三目运算符

e.g.: If(true, func() int { return 1 }, func() int { return 2 }) => 1

func IsDuplicate

func IsDuplicate[T comparable](slice []T) bool

IsDuplicate 是否有重复

e.g.: []int{1, 2, 3, 1} => true

func IsDuplicateCustom

func IsDuplicateCustom(slice []any, equals func(a, b any) bool) bool

IsDuplicateCustom 是否有重复-用于不可比较类型

[⚠️] 性能不高,慎用
e.g.: [1, 2, 3, 1] => true

func IsLittleEndian

func IsLittleEndian() bool

IsLittleEndian 是否小端

func MD5

func MD5(data []byte) string

MD5 生成md5

func MD5File

func MD5File(pathFile string) (md5sum string, err error)

func PBMerge

func PBMerge(src, dst proto.Message)

PBMerge Protobuf - 深拷贝

func PushEventWithTimeout

func PushEventWithTimeout(eventChan chan<- any, event any, timeout time.Duration) error

func RandomInt

func RandomInt(min, max int) int

RandomInt 生成范围内的随机值 [min, max]

func RandomInt64

func RandomInt64(min, max int64) int64

RandomInt64 生成64位随机整数 [min, max]

func RandomString

func RandomString(length uint32) string

RandomString 生成随机字符串

参数:
	length:需要生成的长度

func RandomUint32

func RandomUint32() uint32

RandomUint32 生成32位随机整数

func RandomUint64

func RandomUint64() uint64

RandomUint64 生成64位随机整数

func RandomValueBySlice

func RandomValueBySlice(except, slice []any, equals func(a, b any) bool) any

RandomValueBySlice 生成 随机值

参数:
	except:排除 数据
	slice:从该slice中随机一个,与except不重复
返回值:
	slice 中的值
e.g.: except = [1, 2, 3], slice = [1, 2, 3, 4, 5], 则返回 4 或 5

func RandomWeighted

func RandomWeighted[T IWeight](weights []T) (idx int, err error)

RandomWeighted 从权重中选出序号.[0 ... ]

参数:
	weights:权重
返回值:
	idx:weights 的序号 idx
e.g.: weights = [1, 2, 3], 则返回 0, 1, 2 的概率分别为 1/6, 2/6, 3/6
[❕] 权重为 0 的 数据 不会被选中

func SecureRandomBytes

func SecureRandomBytes(length int) []byte

SecureRandomBytes 生成密码学安全的随机字节 适用场景:密钥、Token、会话ID等安全敏感场景

func SecureRandomInt64

func SecureRandomInt64() int64

SecureRandomInt64 生成密码学安全的64位随机整数

func SecureRandomString

func SecureRandomString(length uint32) string

SecureRandomString 生成密码学安全的随机字符串 适用场景:Token、验证码等安全敏感场景

func SecureRandomUint32

func SecureRandomUint32() uint32

SecureRandomUint32 生成密码学安全的32位随机整数

func SecureRandomUint64

func SecureRandomUint64() uint64

SecureRandomUint64 生成密码学安全的64位随机整数

func Split2Map

func Split2Map[K ISplitKey, V ISplitValue](src, sep1, sep2 string) (map[K]V, error)

Split2Map 拆分字符串, 返回key为 ISplitKey 类型、val为 ISplitValue 类型的map

示例:
	Split2Map[uint32, uint32]("1,10;2,20", ";", ",")    => map[uint32]uint32{1:10, 2:20}
	Split2Map[string, string]("k1,v1;k2,v2", ";", ",")  => map[string]string{"k1":"v1", "k2":"v2"}
	Split2Map[string, int]("min,-100;max,100", ";", ",") => map[string]int{"min":-100, "max":100}

func Split2Slice

func Split2Slice[T ISplitValue](src, sep string) (result []T, err error)

Split2Slice 拆分字符串, 返回 ISplitValue 类型的 slice

示例:
	Split2Slice[uint32]("1,2,3", ",")        => []uint32{1, 2, 3}
	Split2Slice[string]("a,b,c", ",")        => []string{"a", "b", "c"}
	Split2Slice[int]("-1,0,1", ",")          => []int{-1, 0, 1}

func TrimLeftBuffer

func TrimLeftBuffer(buf []byte, trimLen, maxCap int) []byte

TrimLeftBuffer 从左侧裁剪字节切片, 并在全部裁剪且容量过大时重新分配内存

buf: 原字节切片
trimLen: 需要从左侧裁剪的长度
maxCap: 容量阈值, 超过则重新分配
返回: 裁剪后的字节切片

func UUIDRandomBytes

func UUIDRandomBytes() ([16]byte, error)

UUIDRandomBytes 生成UUID字节数组

func UUIDRandomString

func UUIDRandomString() string

UUIDRandomString UUID 生成 随机字符串

Types

type IDisplay

type IDisplay interface {
	String() string
}

type IMapForeach

type IMapForeach interface {
	Foreach(do func(key, value any) (isContinue bool)) // 遍历, isContinue:是否继续遍历
}

type ISplitKey

type ISplitKey interface {
	int | int32 | int64 | uint | uint32 | uint64 | string
}

type ISplitValue

type ISplitValue interface {
	int | int32 | int64 | uint | uint32 | uint64 | string
}

type IWeight

type IWeight interface {
	int | uint32 | uint64
}

type Pair

type Pair[K comparable, V any] struct {
	Key   K
	Value V
}

Jump to

Keyboard shortcuts

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