util

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2024 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrorNotFound = errors.New("not found")

Functions

func Args

func Args(params ...interface{}) []interface{}

func AutoLock

func AutoLock(mu *sync.Mutex, fun func())

 ==================== mutex lock

func AutoRLock

func AutoRLock(mu *sync.RWMutex, fun func())

func AutoWLock

func AutoWLock(mu *sync.RWMutex, fun func())

func BoolComparator

func BoolComparator(a, b bool) int

BoolComparator compare a with b

-1 , if a < b
0  , if a == b
1  , if a > b

func Cast

func Cast(condition bool, trueFun, falseFun func())

func Catch

func Catch(desc string, x interface{}, bFatal ...bool) bool

func Clear

func Clear[M ~map[K]V, K comparable, V any](m M)

Clear removes all entries from m, leaving it empty.

func Clone

func Clone[M ~map[K]V, K comparable, V any](m M) M

Clone returns a copy of m. This is a shallow clone: the new keys and values are set using ordinary assignment.

func Complex128Comparator

func Complex128Comparator(a, b complex128) int

Complex128Comparator compare a with b

-1 , if a < b
0  , if a == b
1  , if a > b

func Complex64Comparator

func Complex64Comparator(a, b complex64) int

Complex64Comparator compare a with b

-1 , if a < b
0  , if a == b
1  , if a > b

func Copy

func Copy[M1 ~map[K]V, M2 ~map[K]V, K comparable, V any](dst M1, src M2)

Copy copies all key/value pairs in src adding them to dst. When a key in src is already present in dst, the value in dst will be overwritten by the value associated with the key in src.

func DefaultVal

func DefaultVal[T any](vars []T) T

func DeleteFunc

func DeleteFunc[M ~map[K]V, K comparable, V any](m M, del func(K, V) bool)

DeleteFunc deletes any key/value pairs from m for which del returns true.

func Equal

func Equal[M1, M2 ~map[K]V, K, V comparable](m1 M1, m2 M2) bool

Equal reports whether two maps contain the same key/value pairs. Values are compared using ==.

func EqualFunc

func EqualFunc[M1 ~map[K]V1, M2 ~map[K]V2, K comparable, V1, V2 any](m1 M1, m2 M2, eq func(V1, V2) bool) bool

EqualFunc is like Equal, but compares values using eq. Keys are still compared with ==.

func ExeFullName

func ExeFullName() string

func ExeName

func ExeName() string

func Float32Comparator

func Float32Comparator(a, b float32) int

Float32Comparator compare a with b

-1 , if a < b
0  , if a == b
1  , if a > b

func Float64Comparator

func Float64Comparator(a, b float64) int

Float64Comparator compare a with b

-1 , if a < b
0  , if a == b
1  , if a > b

func FuncFullName

func FuncFullName(fun interface{}, seps ...rune) string

func FuncFullNameRef

func FuncFullNameRef(valFun reflect.Value, seps ...rune) string

func FuncName

func FuncName(fun interface{}) string

func GoId

func GoId() (int, error)

func Goroutine

func Goroutine(name string, goFun func(), wg ...*sync.WaitGroup)

func Int16Comparator

func Int16Comparator(a, b int16) int

Int16Comparator compare a with b

-1 , if a < b
0  , if a == b
1  , if a > b

func Int32Comparator

func Int32Comparator(a, b int32) int

Int32Comparator compare a with b

-1 , if a < b
0  , if a == b
1  , if a > b

func Int64Comparator

func Int64Comparator(a, b int64) int

Int64Comparator compare a with b

-1 , if a < b
0  , if a == b
1  , if a > b

func Int8Comparator

func Int8Comparator(a, b int8) int

Int8Comparator compare a with b

-1 , if a < b
0  , if a == b
1  , if a > b

func IntComparator

func IntComparator(a, b int) int

IntComparator compare a with b

-1 , if a < b
0  , if a == b
1  , if a > b

func Keys

func Keys[M ~map[K]V, K comparable, V any](m M) []K

Keys returns the keys of the map m. The keys will be in an indeterminate order.

func Lock

func Lock(mu *sync.Mutex) *sync.Mutex

func OrderedTypeCmp

func OrderedTypeCmp[T Ordered](a, b T) int

func Quit

func Quit(delay ...time.Duration)

func RLock

func RLock(mu *sync.RWMutex) *sync.RWMutex

func RUnLock

func RUnLock(rMutex *sync.RWMutex)

func Sleep

func Sleep(d time.Duration)

func Stack

func Stack(depth int) (file string, line int, fun string)

func StringComparator

func StringComparator(a, b string) int

StringComparator compare a with b

-1 , if a < b
0  , if a == b
1  , if a > b

func StructName

func StructName(obj interface{}) string

func Term

func Term(clearHand func(interface{}))

func Tern

func Tern[T bool, U any](isTrue T, ifValue U, elseValue U) U

func TimeLived

func TimeLived() time.Duration

func TimeStart

func TimeStart() time.Time

func Uint16Comparator

func Uint16Comparator(a, b uint16) int

Uint16Comparator compare a with b

-1 , if a < b
0  , if a == b
1  , if a > b

func Uint32Comparator

func Uint32Comparator(a, b uint32) int

Uint32Comparator compare a with b

-1 , if a < b
0  , if a == b
1  , if a > b

func Uint64Comparator

func Uint64Comparator(a, b uint64) int

Uint64Comparator compare a with b

-1 , if a < b
0  , if a == b
1  , if a > b

func Uint8Comparator

func Uint8Comparator(a, b uint8) int

Uint8Comparator compare a with b

-1 , if a < b
0  , if a == b
1  , if a > b

func UintComparator

func UintComparator(a, b uint) int

UintComparator compare a with b

-1 , if a < b
0  , if a == b
1  , if a > b

func UintptrComparator

func UintptrComparator(a, b uintptr) int

UintptrComparator compare a with b

-1 , if a < b
0  , if a == b
1  , if a > b

func UnLock

func UnLock(lockFun *sync.Mutex)

func Values

func Values[M ~map[K]V, K comparable, V any](m M) []V

Values returns the values of the map m. The values will be in an indeterminate order.

func WLock

func WLock(mu *sync.RWMutex) *sync.RWMutex

func WUnLock

func WUnLock(wMutex *sync.RWMutex)

func Wait

func Wait(x ...interface{})

Types

type Comparator

type Comparator[T any] func(a, b T) int

Comparator Should return a number:

-1 , if a < b
0  , if a == b
1  , if a > b

func Reverse

func Reverse[T any](cmp Comparator[T]) Comparator[T]

Reverse returns a comparator reverse to cmp

type Complex

type Complex interface {
	~complex64 | ~complex128
}

type Float

type Float interface {
	~float32 | ~float64
}

type HashMap

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

HashMap uses RbTress for internal data structure, and every key can must bee unique.

func NewHashMap

func NewHashMap[K comparable, V any](opts ...Option) *HashMap[K, V]

New creates a new map

func (*HashMap[K, V]) Clear

func (m *HashMap[K, V]) Clear()

Clear clears the map

func (*HashMap[K, V]) Contains

func (m *HashMap[K, V]) Contains(keys ...K) bool

Contains returns true if the key is in the map. otherwise returns false.

func (*HashMap[K, V]) Erase

func (m *HashMap[K, V]) Erase(key K)

Erase erases the node by the passed key from the map if the key in the Map

func (*HashMap[K, V]) Find

func (m *HashMap[K, V]) Find(key K) *Pair[K, V]

Find finds a node by the passed key and returns its iterator

func (*HashMap[K, V]) Get

func (m *HashMap[K, V]) Get(key K) (V, bool)

Get returns the value of the passed key if the key is in the map, otherwise returns nil

func (*HashMap[K, V]) Got

func (m *HashMap[K, V]) Got(key K) V

Got returns the value of the passed key if the key is in the map, otherwise returns nil

func (*HashMap[K, V]) Insert

func (m *HashMap[K, V]) Insert(key K, value V)

Insert inserts a key-value to the map

func (*HashMap[K, V]) Inserts

func (m *HashMap[K, V]) Inserts(pairs ...*Pair[K, V])

func (*HashMap[K, V]) Iterator

func (m *HashMap[K, V]) Iterator(iterFunc func(key K, val V))

func (*HashMap[K, V]) Keys

func (m *HashMap[K, V]) Keys() []K

Keys return []K

func (*HashMap[K, V]) Replace

func (m *HashMap[K, V]) Replace(data map[K]V)

func (*HashMap[K, V]) Size

func (m *HashMap[K, V]) Size() int

Size returns the amount of elements in the map

func (*HashMap[K, V]) Values

func (m *HashMap[K, V]) Values() []V

Values return []V

type ILocker

type ILocker interface {

	// 主锁
	Lock() interface{}

	UnLock(i interface{})
}

普通锁

type IRWLocker

type IRWLocker interface {

	// 写锁
	ILocker

	// 读锁
	RLock() interface{}

	RUnLock(i interface{})
}

读写锁

type IntStringer

type IntStringer interface {
	Signed

	fmt.Stringer
}

type Integer

type Integer interface {
	Signed | Unsigned
}

type InvalidLocker

type InvalidLocker struct {
}

InvalidLocker is a fake locker

func (InvalidLocker) Lock

func (l InvalidLocker) Lock() interface{}

Lock does nothing

func (InvalidLocker) RLock

func (l InvalidLocker) RLock() interface{}

RLock does nothing

func (InvalidLocker) RUnLock

func (l InvalidLocker) RUnLock(i interface{})

RUnlock does nothing

func (InvalidLocker) UnLock

func (l InvalidLocker) UnLock(i interface{})

Unlock does nothing

type Locker

type Locker struct {
	// contains filtered or unexported fields
}

==================== Locker

func (*Locker) Lock

func (l *Locker) Lock() interface{}

func (*Locker) Mutex

func (l *Locker) Mutex() *sync.Mutex

func (*Locker) UnLock

func (l *Locker) UnLock(i interface{})

type Option

type Option func(option *Options)

Option is a function type used to set Options

func HashMapWithSafe

func HashMapWithSafe() Option

HashMapWithSafe is used to set a map goroutine-safe Note that iterators are not goroutine safe, and it is useless to turn on the setting option here. so don't use iterator in multi goroutines

type Options

type Options struct {
	// contains filtered or unexported fields
}

Options holds Map's options

type Ordered

type Ordered interface {
	Integer | Float | ~string
}

type Pair

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

func MakePair

func MakePair[K comparable, V any](key K, val V) *Pair[K, V]

func (*Pair[K, V]) Equal

func (p *Pair[K, V]) Equal(other *Pair[K, V]) bool

func (*Pair[K, V]) First

func (p *Pair[K, V]) First() K

func (*Pair[K, V]) Second

func (p *Pair[K, V]) Second() V

func (*Pair[K, V]) Set

func (p *Pair[K, V]) Set(key K, val V)

type RWLocker

type RWLocker struct {
	// contains filtered or unexported fields
}

func (*RWLocker) Lock

func (l *RWLocker) Lock() interface{}

func (*RWLocker) Mutex

func (l *RWLocker) Mutex() *sync.RWMutex

func (*RWLocker) RLock

func (l *RWLocker) RLock() interface{}

func (*RWLocker) RUnLock

func (l *RWLocker) RUnLock(i interface{})

func (*RWLocker) UnLock

func (l *RWLocker) UnLock(i interface{})

type Signed

type Signed interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

type UIntStringer

type UIntStringer interface {
	Unsigned

	fmt.Stringer
}

type Unsigned

type Unsigned interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

Jump to

Keyboard shortcuts

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