tools

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package tools 工具箱

Package tools 字节转化

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Args added in v1.2.0

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

Args 参数

func AutoLock added in v1.2.0

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

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

func AutoRLock added in v1.2.0

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

func AutoWLock added in v1.2.0

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

func BoolToBytes

func BoolToBytes(v bool) []byte

BoolToBytes bool->bytes

func BytesToBool

func BytesToBool(buf []byte) bool

BytesToBool bytes->bool

func BytesToFloat32

func BytesToFloat32(bytes []byte) float32

BytesToFloat32 BytesToFloat32

func BytesToFloat64

func BytesToFloat64(bytes []byte) float64

BytesToFloat64 BytesToFloat64

func BytesToInt32

func BytesToInt32(buf []byte) int32

BytesToInt32 BytesToInt32

func BytesToInt64

func BytesToInt64(buf []byte) int64

BytesToInt64 BytesToInt64

func BytesToMap

func BytesToMap(bytes []byte) (map[string]any, error)

BytesToMap BytesToMap

func Cast added in v1.2.0

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

Cast 三元方法

func Catch

func Catch(desc string, x interface{}) error

catch panic

func DefaultVal added in v1.2.0

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

DefaultVal 默认值

func Float32ToBytes

func Float32ToBytes(float float32) []byte

Float32ToBytes Float32ToBytes

func Float64ToBytes

func Float64ToBytes(float float64) []byte

Float64ToBytes Float64ToBytes

func FromBase34 added in v1.1.7

func FromBase34(s string) (uint64, error)

FromBase34 ...

func FromBase62 added in v1.1.7

func FromBase62(str string) int64

FromBase62 解码base62字符串为整数(0表示非法字符或溢出)

func FuncFullName added in v1.2.0

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

func FuncFullNameRef added in v1.2.0

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

func FuncName added in v1.2.0

func FuncName(fun interface{}) string

FuncName

func Int32ToBytes

func Int32ToBytes(i int32) []byte

Int32ToBytes Int32ToBytes

func Int64ToBytes

func Int64ToBytes(i int64) []byte

Int64ToBytes Int64ToBytes

func JSONToMsgPack added in v1.1.7

func JSONToMsgPack(jsonStr string) ([]byte, error)

JSON → MsgPack

func Lock added in v1.2.0

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

func MapToBytes

func MapToBytes(jmap map[string]any) ([]byte, error)

MapToBytes MapToBytes

func MsgPackToJSON added in v1.1.7

func MsgPackToJSON(msgpackData []byte) (string, error)

MsgPack → JSON

func RLock added in v1.2.0

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

func RUnLock added in v1.2.0

func RUnLock(rMutex *sync.RWMutex)

func StructName added in v1.2.0

func StructName(obj interface{}) string

StructName

func Tern added in v1.2.0

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

Cast 三元运算符

func ToBase34 added in v1.1.7

func ToBase34(d uint64) string

ToBase34 ...

func ToBase62 added in v1.1.7

func ToBase62(number int64) string

ToBase62 编码整数为base62字符串

func UnLock added in v1.2.0

func UnLock(lockFun *sync.Mutex)

func WLock added in v1.2.0

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

func WUnLock added in v1.2.0

func WUnLock(wMutex *sync.RWMutex)

Types

type ID

type ID uint64

An ID is a unique, uniformly distributed 64-bit ID.

func GenerateID

func GenerateID() ID

GenerateID returns a randomly-generated 64-bit ID. This function is thread-safe. IDs are produced by consuming an AES-CTR-128 keystream in 64-bit chunks. The AES key is randomly generated on initialization, as is the counter's initial state. On machines with AES-NI support, ID generation takes ~30ns and generates no garbage.

func ParseID

func ParseID(s string) (ID, error)

ParseID parses the given string as a hexadecimal string.

func (ID) MarshalJSON

func (id ID) MarshalJSON() ([]byte, error)

MarshalJSON encodes the ID as a hex string.

func (ID) String

func (id ID) String() string

String returns the ID as a hex string.

func (*ID) UnmarshalJSON

func (id *ID) UnmarshalJSON(data []byte) error

UnmarshalJSON decodes the given data as either a hexadecimal string or JSON integer.

type ILocker added in v1.2.0

type ILocker interface {

	// 主锁
	Lock() interface{}

	UnLock(i interface{})
}

普通锁

type IRWLocker added in v1.2.0

type IRWLocker interface {

	// 写锁
	ILocker

	// 读锁
	RLock() interface{}

	RUnLock(i interface{})
}

读写锁

type InvalidLocker added in v1.2.0

type InvalidLocker struct {
}

InvalidLocker is a fake locker

func (InvalidLocker) Lock added in v1.2.0

func (l InvalidLocker) Lock() interface{}

Lock does nothing

func (InvalidLocker) RLock added in v1.2.0

func (l InvalidLocker) RLock() interface{}

RLock does nothing

func (InvalidLocker) RUnLock added in v1.2.0

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

RUnlock does nothing

func (InvalidLocker) UnLock added in v1.2.0

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

Unlock does nothing

type Locker added in v1.2.0

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

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

func (*Locker) Lock added in v1.2.0

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

func (*Locker) Mutex added in v1.2.0

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

func (*Locker) UnLock added in v1.2.0

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

type Map added in v1.2.0

type Map[K comparable] struct {
	// contains filtered or unexported fields
}

Map is a map with lock

func NewSafeMap added in v1.1.7

func NewSafeMap[K comparable]() *Map[K]

NewSafeMap return new safemap

func (*Map[K]) Check added in v1.2.0

func (m *Map[K]) Check(k K) bool

Check Returns true if k is exist in the map.

func (*Map[K]) Delete added in v1.2.0

func (m *Map[K]) Delete(k K)

Delete the given key and value.

func (*Map[K]) DeleteAll added in v1.2.0

func (m *Map[K]) DeleteAll()

DeleteAll DeleteAll

func (*Map[K]) Get added in v1.2.0

func (m *Map[K]) Get(k K) any

Get from maps return the k's value

func (*Map[K]) Items added in v1.2.0

func (m *Map[K]) Items() map[K]any

Items returns all items in safemap.

func (*Map[K]) Set added in v1.2.0

func (m *Map[K]) Set(k K, v any) bool

Set Maps the given key and value. Returns false if the key is already in the map and changes nothing.

type Queue

type Queue[T any] struct {
	// contains filtered or unexported fields
}

Queue represents a single instance of the queue data structure.

func NewQueue

func NewQueue[T any]() *Queue[T]

NewQueue constructs and returns a new Queue.

func (*Queue[T]) Add

func (q *Queue[T]) Add(elem T)

Add puts an element on the end of the queue.

func (*Queue[T]) Get

func (q *Queue[T]) Get(i int) T

Get returns the element at index i in the queue. If the index is invalid, the call will panic. This method accepts both positive and negative index values. Index 0 refers to the first element, and index -1 refers to the last.

func (*Queue[T]) Length

func (q *Queue[T]) Length() int

Length returns the number of elements currently stored in the queue.

func (*Queue[T]) Peek

func (q *Queue[T]) Peek() T

Peek returns the element at the head of the queue. This call panics if the queue is empty.

func (*Queue[T]) Remove

func (q *Queue[T]) Remove() T

Remove removes and returns the element from the front of the queue. If the queue is empty, the call will panic.

type RWLocker added in v1.2.0

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

func (*RWLocker) Lock added in v1.2.0

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

func (*RWLocker) Mutex added in v1.2.0

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

func (*RWLocker) RLock added in v1.2.0

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

func (*RWLocker) RUnLock added in v1.2.0

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

func (*RWLocker) UnLock added in v1.2.0

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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