Documentation
¶
Index ¶
- func AdjustBufferSize(buf []byte, usedSize, minSpace, initSize int) []byte
- func Float32Equal(a, b float32) bool
- func Float32Less(a, b float32) bool
- func GenToken(prefix string) string
- func GetFuncName(i any, seps ...rune) string
- func GetGoroutineID() uint64
- func HASH32(data []byte) uint32
- func HASH64(data []byte) uint64
- func HexStringToUint32(hexStr string) (uint32, error)
- func If[T any](condition bool, trueFn, falseFn func() T) T
- func IsDuplicate[T comparable](slice []T) bool
- func IsDuplicateCustom(slice []any, equals func(a, b any) bool) bool
- func IsLittleEndian() bool
- func MD5(data []byte) string
- func MD5File(pathFile string) (md5sum string, err error)
- func PBMerge(src, dst proto.Message)
- func PushEventWithTimeout(eventChan chan<- any, event any, timeout time.Duration) error
- func RandomInt(min, max int) int
- func RandomInt64(min, max int64) int64
- func RandomString(length uint32) string
- func RandomUint32() uint32
- func RandomUint64() uint64
- func RandomValueBySlice(except, slice []any, equals func(a, b any) bool) any
- func RandomWeighted[T IWeight](weights []T) (idx int, err error)
- func SecureRandomBytes(length int) []byte
- func SecureRandomInt64() int64
- func SecureRandomString(length uint32) string
- func SecureRandomUint32() uint32
- func SecureRandomUint64() uint64
- func Split2Map[K ISplitKey, V ISplitValue](src, sep1, sep2 string) (map[K]V, error)
- func Split2Slice[T ISplitValue](src, sep string) (result []T, err error)
- func TrimLeftBuffer(buf []byte, trimLen, maxCap int) []byte
- func UUIDRandomBytes() ([16]byte, error)
- func UUIDRandomString() string
- type IDisplay
- type IMapForeach
- type ISplitKey
- type ISplitValue
- type IWeight
- type Pair
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AdjustBufferSize ¶
AdjustBufferSize 调整缓冲区大小
扩容策略: 如果剩余空间小于 minSpace, 则扩容 缩容策略: 如果使用的大小, 小于缓冲区大小的1/4, 并且initSize小于缓冲区大小, 则缩容为缓冲区大小的一半 参数: buf: 原缓冲区 usedSize: 使用的大小 minSpace: 最小剩余空间要求(通常为1024) initSize: 初始缓冲区大小(通常为2048) 返回: []byte: 调整后的缓冲区
func Float32Less ¶
func IsDuplicate ¶
func IsDuplicate[T comparable](slice []T) bool
IsDuplicate 是否有重复
e.g.: []int{1, 2, 3, 1} => true
func PushEventWithTimeout ¶
func RandomValueBySlice ¶
RandomValueBySlice 生成 随机值
参数: except:排除 数据 slice:从该slice中随机一个,与except不重复 返回值: slice 中的值 e.g.: except = [1, 2, 3], slice = [1, 2, 3, 4, 5], 则返回 4 或 5
func RandomWeighted ¶
RandomWeighted 从权重中选出序号.[0 ... ]
参数: weights:权重 返回值: idx:weights 的序号 idx e.g.: weights = [1, 2, 3], 则返回 0, 1, 2 的概率分别为 1/6, 2/6, 3/6 [❕] 权重为 0 的 数据 不会被选中
func SecureRandomBytes ¶
SecureRandomBytes 生成密码学安全的随机字节 适用场景:密钥、Token、会话ID等安全敏感场景
func SecureRandomString ¶
SecureRandomString 生成密码学安全的随机字符串 适用场景:Token、验证码等安全敏感场景
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 ¶
TrimLeftBuffer 从左侧裁剪字节切片, 并在全部裁剪且容量过大时重新分配内存
buf: 原字节切片 trimLen: 需要从左侧裁剪的长度 maxCap: 容量阈值, 超过则重新分配 返回: 裁剪后的字节切片
Types ¶
type IMapForeach ¶
type ISplitValue ¶
type Pair ¶
type Pair[K comparable, V any] struct { Key K Value V }
Click to show internal directories.
Click to hide internal directories.