Documentation ¶
Index ¶
- func BoolToString(value bool) string
- func ByteToString(value byte) string
- func BytesToString(b []byte) string
- func CompareVersion(version1, version2 string) int
- func ConditionalRetryByExponentialBackoff(f func() error, cond func() bool, maxRetries int, ...) error
- func Convert[A, B any](src A) B
- func Float32ToString(value float32) string
- func Float64ToString(value float64) string
- func GetError(err error) (int, error)
- func GoFormat(filePath string)
- func Handle(f func())
- func HandleErr(f func() error) error
- func HandleV[V any](v V, f func(v V))
- func Hostname() string
- func If[T any](expression bool, t T, f T) T
- func Int16ToString(value int16) string
- func Int32ToString(value int32) string
- func Int64ToString(value int64) string
- func Int8ToString(value int8) string
- func IntToString(value int) string
- func IsNumber(v any) bool
- func LaunchTime() time.Time
- func MarshalIndentJSON(v interface{}, prefix, indent string) []byte
- func MarshalJSON(v interface{}) []byte
- func MarshalJSONE(v interface{}) ([]byte, error)
- func MarshalToTargetWithJSON(src, dest interface{}) error
- func NumberToRome(num int) string
- func OldVersion(version1, version2 string) bool
- func PID() int
- func RecoverTransform(a any) error
- func RegError(code int, message string) error
- func RegErrorRef(code int, message string, ref error) error
- func Retry(count int, interval time.Duration, f func() error) error
- func RetryAsync(count int, interval time.Duration, f func() error, callback func(err error))
- func RetryByExponentialBackoff(f func() error, maxRetries int, baseDelay, maxDelay time.Duration, ...) error
- func RetryByRule(f func() error, rule func(count int) time.Duration) error
- func RetryForever(interval time.Duration, f func() error)
- func RuneToString(value rune) string
- func SliceToString(value []string) string
- func StringToBool(value string) bool
- func StringToByte(value string) byte
- func StringToBytes(s string) []byte
- func StringToFloat32(value string) float32
- func StringToFloat64(value string) float64
- func StringToInt(value string) int
- func StringToInt16(value string) int16
- func StringToInt32(value string) int32
- func StringToInt64(value string) int64
- func StringToInt8(value string) int8
- func StringToRune(value string) rune
- func StringToSlice(value string) []string
- func StringToUint(value string) uint
- func StringToUint16(value string) uint16
- func StringToUint32(value string) uint32
- func StringToUint64(value string) uint64
- func StringToUint8(value string) uint8
- func TryReadChannel[T any](ch <-chan T) (v T, suc bool)
- func TryReadChannelByHandler[T any](ch <-chan T, handler func(ch <-chan T) T) (v T)
- func TryWriteChannel[T any](ch chan<- T, data T) bool
- func TryWriteChannelByHandler[T any](ch chan<- T, data T, handler func())
- func Uint16ToString(value uint16) string
- func Uint32ToString(value uint32) string
- func Uint64ToString(value uint64) string
- func Uint8ToString(value uint8) string
- func UintToString(value uint) string
- func UnmarshalJSON(data []byte, v interface{}) error
- type BitSet
- func (slf *BitSet[Bit]) And(other *BitSet[Bit]) *BitSet[Bit]
- func (slf *BitSet[Bit]) Bits() []Bit
- func (slf *BitSet[Bit]) Cap() int
- func (slf *BitSet[Bit]) Clear() *BitSet[Bit]
- func (slf *BitSet[Bit]) Clone() *BitSet[Bit]
- func (slf *BitSet[Bit]) Complement(other *BitSet[Bit]) bool
- func (slf *BitSet[Bit]) Contains(other *BitSet[Bit]) bool
- func (slf *BitSet[Bit]) ContainsAll(other *BitSet[Bit]) bool
- func (slf *BitSet[Bit]) ContainsAny(other *BitSet[Bit]) bool
- func (slf *BitSet[Bit]) Del(bit Bit) *BitSet[Bit]
- func (slf *BitSet[Bit]) Difference(other *BitSet[Bit]) bool
- func (slf *BitSet[Bit]) Equal(other *BitSet[Bit]) bool
- func (slf *BitSet[Bit]) Has(bit Bit) bool
- func (slf *BitSet[Bit]) Intersect(other *BitSet[Bit]) bool
- func (slf *BitSet[Bit]) IsZero() bool
- func (slf *BitSet[Bit]) Len() int
- func (slf *BitSet[Bit]) MarshalJSON() ([]byte, error)
- func (slf *BitSet[Bit]) Max() Bit
- func (slf *BitSet[Bit]) Min() Bit
- func (slf *BitSet[Bit]) Not() *BitSet[Bit]
- func (slf *BitSet[Bit]) Or(other *BitSet[Bit]) *BitSet[Bit]
- func (slf *BitSet[Bit]) Reverse() *BitSet[Bit]
- func (slf *BitSet[Bit]) Set(bit Bit) *BitSet[Bit]
- func (slf *BitSet[Bit]) Shrink() *BitSet[Bit]
- func (slf *BitSet[Bit]) String() string
- func (slf *BitSet[Bit]) Sub(other *BitSet[Bit]) *BitSet[Bit]
- func (slf *BitSet[Bit]) Subset(other *BitSet[Bit]) bool
- func (slf *BitSet[Bit]) Superset(other *BitSet[Bit]) bool
- func (slf *BitSet[Bit]) SymmetricDifference(other *BitSet[Bit]) bool
- func (slf *BitSet[Bit]) Union(other *BitSet[Bit]) bool
- func (slf *BitSet[Bit]) UnmarshalJSON(data []byte) error
- func (slf *BitSet[Bit]) Xor(other *BitSet[Bit]) *BitSet[Bit]
- type LossCounter
- type Matcher
- type Permission
- func (slf *Permission[Code, EntityID]) AddPermission(entityId EntityID, permission ...Code)
- func (slf *Permission[Code, EntityID]) HasPermission(entityId EntityID, permission Code) bool
- func (slf *Permission[Code, EntityID]) RemovePermission(entityId EntityID, permission ...Code)
- func (slf *Permission[Code, EntityID]) SetPermission(entityId EntityID, permission ...Code)
- type StackGo
- type VerifyHandle
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BytesToString ¶ added in v0.0.20
BytesToString 以零拷贝的方式将字节切片转换为字符串
func CompareVersion ¶ added in v0.3.4
CompareVersion 返回一个整数,用于表示两个版本号的比较结果:
- 如果 version1 大于 version2,它将返回 1
- 如果 version1 小于 version2,它将返回 -1
- 如果 version1 和 version2 相等,它将返回 0
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/super" ) func main() { result := super.CompareVersion("1.2.3", "1.2.2") fmt.Println(result) }
Output: 1
func ConditionalRetryByExponentialBackoff ¶ added in v0.3.1
func ConditionalRetryByExponentialBackoff(f func() error, cond func() bool, maxRetries int, baseDelay, maxDelay time.Duration, multiplier, randomization float64, ignoreErrors ...error) error
ConditionalRetryByExponentialBackoff 该函数与 RetryByExponentialBackoff 类似,但是可以被中断
- cond 为中断条件,当 cond 返回 false 时,将会中断重试
该函数通常用于在重试过程中,需要中断重试的场景,例如:
- 用户请求开始游戏,由于网络等情况,进入重试状态。此时用户再次发送开始游戏请求,此时需要中断之前的重试,避免重复进入游戏
func Convert ¶ added in v0.0.22
func Convert[A, B any](src A) B
Convert 以零拷贝的方式将一个对象转换为另一个对象
- 两个对象字段必须完全一致
- 该函数可以绕过私有字段的访问限制
func Float32ToString ¶ added in v0.0.31
Float32ToString float32 转换为字符串
func Float64ToString ¶ added in v0.0.31
Float64ToString float64 转换为字符串
func HandleV ¶ added in v0.0.8
func HandleV[V any](v V, f func(v V))
HandleV 执行 f 函数,如果 f 为 nil,则不执行
func Int16ToString ¶ added in v0.0.31
Int16ToString int16 转换为字符串
func Int32ToString ¶ added in v0.0.31
Int32ToString int32 转换为字符串
func Int64ToString ¶ added in v0.0.31
Int64ToString int64 转换为字符串
func MarshalIndentJSON ¶ added in v0.0.21
MarshalIndentJSON 将对象转换为 json
func MarshalJSON ¶ added in v0.0.21
func MarshalJSON(v interface{}) []byte
MarshalJSON 将对象转换为 json
- 当转换失败时,将返回 json 格式的空对象
func MarshalToTargetWithJSON ¶ added in v0.2.3
func MarshalToTargetWithJSON(src, dest interface{}) error
MarshalToTargetWithJSON 将对象转换为目标对象
func OldVersion ¶ added in v0.3.4
OldVersion 检查 version2 对于 version1 来说是不是旧版本
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/super" ) func main() { result := super.OldVersion("1.2.3", "1.2.2") fmt.Println(result) }
Output: true
func RecoverTransform ¶ added in v0.4.2
RecoverTransform recover 错误转换
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/super" ) func main() { defer func() { if err := super.RecoverTransform(recover()); err != nil { fmt.Println(err) } }() panic("test") }
Output: test
func Retry ¶ added in v0.0.8
Retry 根据提供的 count 次数尝试执行 f 函数,如果 f 函数返回错误,则在 interval 后重试,直到成功或者达到 count 次数
func RetryAsync ¶ added in v0.0.8
RetryAsync 与 Retry 类似,但是是异步执行
- 传入的 callback 函数会在执行完毕后被调用,如果执行成功,则 err 为 nil,否则为错误信息
- 如果 callback 为 nil,则不会在执行完毕后被调用
func RetryByExponentialBackoff ¶ added in v0.2.9
func RetryByExponentialBackoff(f func() error, maxRetries int, baseDelay, maxDelay time.Duration, multiplier, randomization float64, ignoreErrors ...error) error
RetryByExponentialBackoff 根据指数退避算法尝试执行 f 函数
- maxRetries:最大重试次数
- baseDelay:基础延迟
- maxDelay:最大延迟
- multiplier:延迟时间的乘数,通常为 2
- randomization:延迟时间的随机化因子,通常为 0.5
- ignoreErrors:忽略的错误,当 f 返回的错误在 ignoreErrors 中时,将不会进行重试
func RetryByRule ¶ added in v0.2.9
RetryByRule 根据提供的规则尝试执行 f 函数,如果 f 函数返回错误,则根据 rule 的返回值进行重试
- rule 将包含一个入参,表示第几次重试,返回值表示下一次重试的时间间隔,当返回值为 0 时,表示不再重试
- rule 的 count 将在 f 首次失败后变为 1,因此 rule 的入参将从 1 开始
func RetryForever ¶ added in v0.2.2
RetryForever 根据提供的 interval 时间间隔尝试执行 f 函数,如果 f 函数返回错误,则在 interval 后重试,直到成功
func SliceToString ¶ added in v0.0.31
SliceToString 切片转换为字符串
func StringToBytes ¶ added in v0.0.20
StringToBytes 以零拷贝的方式将字符串转换为字节切片
func StringToFloat32 ¶ added in v0.0.31
StringToFloat32 字符串转换为 float32
func StringToFloat64 ¶ added in v0.0.31
StringToFloat64 字符串转换为 float64
func StringToInt16 ¶ added in v0.0.31
StringToInt16 字符串转换为 int16
func StringToInt32 ¶ added in v0.0.31
StringToInt32 字符串转换为 int32
func StringToInt64 ¶ added in v0.0.31
StringToInt64 字符串转换为 int64
func StringToSlice ¶ added in v0.0.31
StringToSlice 字符串转换为切片
func StringToUint16 ¶ added in v0.0.31
StringToUint16 字符串转换为 uint16
func StringToUint32 ¶ added in v0.0.31
StringToUint32 字符串转换为 uint32
func StringToUint64 ¶ added in v0.0.31
StringToUint64 字符串转换为 uint64
func StringToUint8 ¶ added in v0.0.31
StringToUint8 字符串转换为 uint8
func TryReadChannel ¶ added in v0.5.2
TryReadChannel 尝试读取 channel,如果 channel 无法读取则忽略,返回是否读取成功
- 无法读取的情况包括:channel 已空、channel 已关闭
func TryReadChannelByHandler ¶ added in v0.5.2
func TryReadChannelByHandler[T any](ch <-chan T, handler func(ch <-chan T) T) (v T)
TryReadChannelByHandler 尝试读取 channel,如果 channel 无法读取则执行 handler
- 无法读取的情况包括:channel 已空、channel 已关闭
func TryWriteChannel ¶ added in v0.4.4
TryWriteChannel 尝试写入 channel,如果 channel 无法写入则忽略,返回是否写入成功
- 无法写入的情况包括:channel 已满、channel 已关闭
func TryWriteChannelByHandler ¶ added in v0.4.4
func TryWriteChannelByHandler[T any](ch chan<- T, data T, handler func())
TryWriteChannelByHandler 尝试写入 channel,如果 channel 无法写入则执行 handler
- 无法写入的情况包括:channel 已满、channel 已关闭
func Uint16ToString ¶ added in v0.0.31
Uint16ToString uint16 转换为字符串
func Uint32ToString ¶ added in v0.0.31
Uint32ToString uint32 转换为字符串
func Uint64ToString ¶ added in v0.0.31
Uint64ToString uint64 转换为字符串
func Uint8ToString ¶ added in v0.0.31
Uint8ToString uint8 转换为字符串
func UnmarshalJSON ¶ added in v0.0.21
UnmarshalJSON 将 json 转换为对象
Types ¶
type BitSet ¶ added in v0.3.6
BitSet 是一个可以动态增长的比特位集合
- 默认情况下将使用 64 位无符号整数来表示比特位,当需要表示的比特位超过 64 位时,将自动增长
func NewBitSet ¶ added in v0.3.6
NewBitSet 通过指定的 Bit 位创建一个 BitSet
- 当指定的 Bit 位存在负数时,将会 panic
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/super" ) func main() { var bs = super.NewBitSet(1, 2, 3, 4, 5, 6, 7, 8, 9) bs.Set(10) fmt.Println(bs.Bits()) }
Output: [1 2 3 4 5 6 7 8 9 10]
func (*BitSet[Bit]) Bits ¶ added in v0.3.6
func (slf *BitSet[Bit]) Bits() []Bit
Bits 返回当前 BitSet 中被设置的比特位
func (*BitSet[Bit]) Cap ¶ added in v0.3.6
Cap 返回当前 BitSet 中可以表示的最大比特位数量
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/super" ) func main() { var bs = super.NewBitSet(63) fmt.Println(bs.Cap()) }
Output: 64
func (*BitSet[Bit]) Clear ¶ added in v0.3.6
Clear 清空所有的比特位
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/super" ) func main() { var bs = super.NewBitSet(1, 2, 3, 4, 5, 6, 7, 8, 9) bs.Clear() fmt.Println(bs.Bits()) }
Output: []
func (*BitSet[Bit]) Complement ¶ added in v0.3.6
Complement 检查当前 BitSet 是否为另一个 BitSet 的补集
func (*BitSet[Bit]) ContainsAll ¶ added in v0.3.6
ContainsAll 检查当前 BitSet 是否包含另一个 BitSet 中的所有比特位
func (*BitSet[Bit]) ContainsAny ¶ added in v0.3.6
ContainsAny 检查当前 BitSet 是否包含另一个 BitSet 中的任意比特位
func (*BitSet[Bit]) Del ¶ added in v0.3.6
Del 将指定的位 bit 设置为 0
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/super" ) func main() { var bs = super.NewBitSet(1, 2, 3, 4, 5, 6, 7, 8, 9) bs.Del(1) fmt.Println(bs.Bits()) }
Output: [2 3 4 5 6 7 8 9]
func (*BitSet[Bit]) Difference ¶ added in v0.3.6
Difference 检查当前 BitSet 是否与另一个 BitSet 有差集
func (*BitSet[Bit]) Has ¶ added in v0.3.6
Has 检查指定的位 bit 是否被设置为 1
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/super" ) func main() { var bs = super.NewBitSet(1, 2, 3, 4, 5, 6, 7, 8, 9) fmt.Println(bs.Has(1)) fmt.Println(bs.Has(10)) }
Output: true false
func (*BitSet[Bit]) Len ¶ added in v0.3.6
Len 返回当前 BitSet 中被设置的比特位数量
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/super" ) func main() { var bs = super.NewBitSet(1, 2, 3, 4, 5, 6, 7, 8, 9) fmt.Println(bs.Len()) }
Output: 9
func (*BitSet[Bit]) MarshalJSON ¶ added in v0.3.6
MarshalJSON 实现 json.Marshaler 接口
func (*BitSet[Bit]) Set ¶ added in v0.3.6
Set 将指定的位 bit 设置为 1
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/super" ) func main() { var bs = super.NewBitSet[int]() bs.Set(10) fmt.Println(bs.Bits()) }
Output: [10]
func (*BitSet[Bit]) Shrink ¶ added in v0.3.6
Shrink 将 BitSet 中的比特位集合缩小到最小
- 正常情况下当 BitSet 中的比特位超出 64 位时,将自动增长,当 BitSet 中的比特位数量减少时,可以使用该方法将 BitSet 中的比特位集合缩小到最小
Example ¶
package main import ( "fmt" "github.com/kercylan98/minotaur/utils/super" ) func main() { var bs = super.NewBitSet(111, 222, 333, 444) fmt.Println(bs.Cap()) bs.Del(444) fmt.Println(bs.Cap()) bs.Shrink() fmt.Println(bs.Cap()) }
Output: 448 448 384
func (*BitSet[Bit]) SymmetricDifference ¶ added in v0.3.6
SymmetricDifference 检查当前 BitSet 是否与另一个 BitSet 有对称差集
func (*BitSet[Bit]) UnmarshalJSON ¶ added in v0.3.6
UnmarshalJSON 实现 json.Unmarshaler 接口
type LossCounter ¶ added in v0.2.7
type LossCounter struct {
// contains filtered or unexported fields
}
func StartLossCounter ¶ added in v0.2.7
func StartLossCounter() *LossCounter
StartLossCounter 开始损耗计数
func (*LossCounter) GetLoss ¶ added in v0.2.7
func (slf *LossCounter) GetLoss(handler func(step int, name string, loss time.Duration))
GetLoss 获取损耗
func (*LossCounter) Record ¶ added in v0.2.7
func (slf *LossCounter) Record(name string)
Record 记录一次损耗
func (*LossCounter) String ¶ added in v0.3.0
func (slf *LossCounter) String() string
type Matcher ¶ added in v0.0.16
type Matcher[Value, Result any] struct { // contains filtered or unexported fields }
Matcher 匹配器
type Permission ¶ added in v0.2.6
type Permission[Code generic.Integer, EntityID comparable] struct { // contains filtered or unexported fields }
func NewPermission ¶ added in v0.2.6
func NewPermission[Code generic.Integer, EntityID comparable]() *Permission[Code, EntityID]
NewPermission 创建权限
func (*Permission[Code, EntityID]) AddPermission ¶ added in v0.2.6
func (slf *Permission[Code, EntityID]) AddPermission(entityId EntityID, permission ...Code)
AddPermission 添加权限
func (*Permission[Code, EntityID]) HasPermission ¶ added in v0.2.6
func (slf *Permission[Code, EntityID]) HasPermission(entityId EntityID, permission Code) bool
HasPermission 是否有权限
func (*Permission[Code, EntityID]) RemovePermission ¶ added in v0.2.6
func (slf *Permission[Code, EntityID]) RemovePermission(entityId EntityID, permission ...Code)
RemovePermission 移除权限
func (*Permission[Code, EntityID]) SetPermission ¶ added in v0.2.6
func (slf *Permission[Code, EntityID]) SetPermission(entityId EntityID, permission ...Code)
SetPermission 设置权限
type StackGo ¶ added in v0.0.13
type StackGo struct {
// contains filtered or unexported fields
}
StackGo 用于获取上一个协程调用的堆栈信息
- 应当最先运行 Wait 函数,然后在其他协程中调用 Stack 函数或者 GiveUp 函数
- 适用于跨协程同步通讯,例如单线程的消息处理统计耗时打印堆栈信息
func (*StackGo) GiveUp ¶ added in v0.0.13
func (slf *StackGo) GiveUp()
GiveUp 放弃收集消息堆栈
- 在调用 Wait 函数后调用该函数,将会放弃收集消息堆栈并且释放资源
- 在调用 GiveUp 函数后调用 Stack 函数,将会 panic
type VerifyHandle ¶ added in v0.0.23
type VerifyHandle[V any] struct { // contains filtered or unexported fields }
VerifyHandle 校验句柄
func Verify ¶ added in v0.0.23
func Verify[V any](handle func(V)) *VerifyHandle[V]
Verify 对特定表达式进行校验,当表达式不成立时,将执行 handle
Example ¶
package main import ( "errors" "fmt" "github.com/kercylan98/minotaur/utils/super" ) func main() { var getId = func() int { return 1 } var n *super.VerifyHandle[int] super.Verify(func(err error) { fmt.Println(err) }).Case(getId() == 1, errors.New("id can't be 1")). Do() super.Verify(func(err error) { fmt.Println(err) }).PreCase(func() bool { return n == nil }, errors.New("n can't be nil"), func(verify *super.VerifyHandle[error]) bool { return verify.Do() }) }
Output: id can't be 1 n can't be nil
func (*VerifyHandle[V]) Case ¶ added in v0.0.23
func (slf *VerifyHandle[V]) Case(expression bool, value V) *VerifyHandle[V]
Case 校验用例,当 expression 成立时,将忽略后续 Case,并将在 Do 时执行 handle,返回 false
func (*VerifyHandle[V]) Do ¶ added in v0.0.23
func (slf *VerifyHandle[V]) Do() bool
Do 执行校验,当校验失败时,将执行 handle,并返回 false
func (*VerifyHandle[V]) PreCase ¶ added in v0.0.23
func (slf *VerifyHandle[V]) PreCase(expression func() bool, value V, caseHandle func(verify *VerifyHandle[V]) bool) bool
PreCase 先决校验用例,当 expression 成立时,将跳过 caseHandle 的执行,直接执行 handle 并返回 false
- 常用于对前置参数的空指针校验,例如当 a 为 nil 时,不执行 a.B(),而是直接返回 false