util

package
v0.0.0-...-6785f5e Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2025 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetFuncName

func GetFuncName(i interface{}, seps ...rune) string

GetFuncName 获取函数名称

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)

func If

func If(condition bool, trueVal interface{}, falseVal interface{}) interface{}

If 三目运算符 [NOTE] 传递的实参,会在调用时计算所有参数 e.g.: If(true, 1, 2) => 1

func IsDuplicate

func IsDuplicate(slice []interface{}, equals func(a, b interface{}) bool) bool

IsDuplicate 是否有重复 e.g.: [1, 2, 3, 1] => true

func IsLittleEndian

func IsLittleEndian() bool

IsLittleEndian 是否小端

func IsNetErrClosing

func IsNetErrClosing(err error) bool

IsNetErrClosing checks if a network error is due to a closed connection.

func IsNetErrorTemporary

func IsNetErrorTemporary(err error) bool

IsNetErrorTemporary checks if a network error is temporary. [NOTE] 不建议使用

func IsNetErrorTimeout

func IsNetErrorTimeout(err error) bool

IsNetErrorTimeout checks if a network error is a timeout.

func MD5File

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

func Md5

func Md5(data []byte) (md5sum string)

Md5 生成md5

func MutableCopy

func MutableCopy(src proto.Message, dst proto.Message) error

MutableCopy 深拷贝

func PushEventWithTimeout

func PushEventWithTimeout(eventChan chan<- interface{}, event interface{}, timeout time.Duration) error

todo menglc 每次创建定时器, 性能堪忧, 需要优化

func RandomInt

func RandomInt(min int, max int) int

RandomInt 生成范围内的随机值

参数:
	min:最小值
	max:最大值

func RandomString

func RandomString(len uint32) (container string, err error)

RandomString 生成随机字符串

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

func RandomValueBySlice

func RandomValueBySlice(except []interface{}, slice []interface{}, equals func(a, b interface{}) bool) (interface{}, error)

RandomValueBySlice 生成 随机值

参数:
	except:排除 数据
	slice:从该slice中随机一个,与except不重复
返回值:
	slice 中的值

e.g.: except = [1, 2, 3], slice = [1, 2, 3, 4, 5], 则返回 4 或 5

func RandomWeighted

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

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

[NOTE] 参数 weights 必须有长度
参数:
	weights:权重
返回值:
	idx:weights 的序号 idx

e.g.: weights = [1, 2, 3], 则返回 0, 1, 2 的概率分别为 1/6, 2/6, 3/6

func RearRangeData

func RearRangeData(dataSlice []byte, removeLen int, resetCnt int) []byte

RearRangeData 重新整理-数据 removeLen: 移除长度 resetCnt: 重置长度, 大于该长度则重新创建新的数据单元

func Split2MapU32U32

func Split2MapU32U32(s string, sep1 string, sep2 string) (map[uint32]uint32, error)

Split2MapU32U32 拆分字符串, 返回key为uint32类型、val为uint32类型的map e.g.: "1,2;3,4" => map[uint32]int64{1:2, 3:4}

func Split2SliceU32

func Split2SliceU32(s string, sep string) (u32Slice []uint32, err error)

Split2SliceU32 拆分字符串, 返回 uint32 类型的 slice e.g.: "1,2,3" => []uint32{1, 2, 3}

Types

type IDisplay

type IDisplay interface {
	String() string
}

type IObject

type IObject interface {
}

type MapMgr

type MapMgr[TKey comparable, TVal interface{}] struct {
	// contains filtered or unexported fields
}

func NewMapMgr

func NewMapMgr[TKey comparable, TVal interface{}]() *MapMgr[TKey, TVal]

NewMapMgr 创建 Mgr 实例

func (*MapMgr[TKey, TVal]) Add

func (p *MapMgr[TKey, TVal]) Add(key TKey, value TVal)

Add 添加元素

func (*MapMgr[TKey, TVal]) Del

func (p *MapMgr[TKey, TVal]) Del(key TKey)

Del 删除元素

func (*MapMgr[TKey, TVal]) Find

func (p *MapMgr[TKey, TVal]) Find(key TKey) (TVal, bool)

Find 查找元素

type OrderedMap

type OrderedMap[K comparable, V any] struct {
	// contains filtered or unexported fields
}

OrderedMap 是一个泛型结构体,它结合了 slice 和 map 来保持元素顺序并提供快速查找 [NOTE]当从 keys 切片中删除元素时,使用了切片的拼接操作来保持剩余元素的顺序。 这个操作在切片较大时可能效率不高,但在大多数情况下是可以接受的。 如果需要频繁地删除元素,并且关心性能,需要考虑使用其他数据结构,如链表或双向队列,来维护键的顺序。

func NewOrderedMap

func NewOrderedMap[K comparable, V any]() *OrderedMap[K, V]

NewOrderedMap 创建一个新的 OrderedMap 实例

func (*OrderedMap[K, V]) Delete

func (p *OrderedMap[K, V]) Delete(key K)

Delete 从 OrderedMap 中删除一个键值对

func (*OrderedMap[K, V]) Get

func (p *OrderedMap[K, V]) Get(key K) (V, bool)

Get 从 OrderedMap 中根据键获取值

func (*OrderedMap[K, V]) Len

func (p *OrderedMap[K, V]) Len() int

Len 元素个数

func (*OrderedMap[K, V]) Range

func (p *OrderedMap[K, V]) Range(fn func(key K, value V) bool)

Range 迭代 OrderedMap 中的键值对,按插入顺序

func (*OrderedMap[K, V]) Set

func (p *OrderedMap[K, V]) Set(key K, value V)

Set 向 OrderedMap 中添加一个键值对

Jump to

Keyboard shortcuts

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