util

package
v0.3.37 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2023 License: MIT Imports: 37 Imported by: 11

Documentation

Index

Constants

View Source
const RegExpCh = `[一-龥]`

Variables

View Source
var (
	// Epoch is set to the twitter snowflake epoch of Nov 04 2010 01:42:54 UTC in milliseconds
	// You may customize this to set a different epoch for your application.
	Epoch int64 = 1420070400000

	// NodeBits holds the number of bits to use for Snowflake
	// Remember, you have a total 22 bits to share between Snowflake/Step
	NodeBits uint8 = 10

	// StepBits holds the number of bits to use for Step
	// Remember, you have a total 22 bits to share between Snowflake/Step
	StepBits uint8 = 12
)
View Source
var ErrInvalidBase32 = errors.New("invalid base32")

ErrInvalidBase32 is returned by ParseBase32 when given an invalid []byte

View Source
var ErrInvalidBase58 = errors.New("invalid base58")

ErrInvalidBase58 is returned by ParseBase58 when given an invalid []byte

View Source
var FlagUtil = &flagUtil{
	items: map[string]*flagValue{},
}
View Source
var Time2028 = time.Date(2028, 1, 1, 0, 0, 0, 0, time.Local)

Functions

func Apply

func Apply(f interface{}, args []interface{}) []reflect.Value

Apply a function with parameters

func Binary2Byte

func Binary2Byte(in string) uint8

func Binary2Short

func Binary2Short(in string) uint16

func CheckDuplicate

func CheckDuplicate(slice interface{}) bool

func CheckDuplicateString

func CheckDuplicateString(slice []string) bool

func CheckTimeFormat

func CheckTimeFormat(src, layout string) bool

func ConfDecode

func ConfDecode(out interface{}, str string) error

func ConfFormat

func ConfFormat(str string) string

func CreateFile

func CreateFile(filePath string, perms ...int) error

func CurrentPath added in v0.2.9

func CurrentPath() (string, error)

func DigitalNum

func DigitalNum(num int) int

func DigitalToString

func DigitalToString(num int, digital int) string

func ExecPath

func ExecPath() (string, error)

func FilterFileWithExt

func FilterFileWithExt(files []string, ext ...string) []string

func FilterFileWithFunc

func FilterFileWithFunc(files []string, f func(string) bool) []string

func FindFile

func FindFile(dirPath string, name string, recursive bool) string

func FormatTimeToDayString

func FormatTimeToDayString(time time.Time) string

func FormatTimeToISOString added in v0.1.24

func FormatTimeToISOString(time time.Time) string

func FormatTimeToMonthString

func FormatTimeToMonthString(time time.Time) string

func FormatTimeToString

func FormatTimeToString(time time.Time) string

func FormatTimeToYearString

func FormatTimeToYearString(time time.Time) string

func GetCurrentDirectory added in v0.1.24

func GetCurrentDirectory() string

func GetDateNoOffset

func GetDateNoOffset(t time.Time) string

func GetDateWithOffset

func GetDateWithOffset(t time.Time, offset time.Duration) string

func GetDayStart added in v0.2.31

func GetDayStart(t time.Time) time.Time

func GetDaysOfMonth added in v0.2.30

func GetDaysOfMonth(t time.Time) int

获取本月有几天

func GetGoroutineID

func GetGoroutineID() uint64

func GetIpFromAddr

func GetIpFromAddr(addr net.Addr) net.IP

func GetIpWithPrefix added in v0.3.36

func GetIpWithPrefix(prefix string) net.IP

func GetLastDayOfMonth added in v0.2.30

func GetLastDayOfMonth(t time.Time) time.Time

func GetLastDaysOfMonth added in v0.2.31

func GetLastDaysOfMonth(t time.Time, days int) time.Time

func GetLastWeekDay added in v0.2.30

func GetLastWeekDay(t time.Time, weekday time.Weekday) time.Time

func GetLocalIp added in v0.3.36

func GetLocalIp() net.IP

func GetMonthDayByLastWeekDay added in v0.2.30

func GetMonthDayByLastWeekDay(t time.Time, weekday time.Weekday) int

func GetMonthStart added in v0.2.31

func GetMonthStart(t time.Time) time.Time

func GetMonthStartEnd added in v0.2.30

func GetMonthStartEnd(t time.Time) (time.Time, time.Time)

获取本月的起始时间

func GetNextDayStart added in v0.2.31

func GetNextDayStart(t time.Time) time.Time

func GetNextMonthStart added in v0.2.31

func GetNextMonthStart(t time.Time) time.Time

func GetNextMonthsStart added in v0.2.31

func GetNextMonthsStart(t time.Time, months int) time.Time

func GetNextYearStart added in v0.2.31

func GetNextYearStart(t time.Time) time.Time

func GetNowDateWithOffset

func GetNowDateWithOffset(offset time.Duration) string

GetNowDateWithOffset 得到带重置时间的当天的YYYYMMHH

func GetParentDirectory added in v0.1.24

func GetParentDirectory(dir string) string

func GetPassedDays

func GetPassedDays(tm1, tm2 time.Time) int64

等到tm1-tm2的天数,

func GetPublicIP added in v0.3.36

func GetPublicIP() net.IP

func GetTimeFromMs

func GetTimeFromMs(t int64) time.Time

func GetTimeWithOffset

func GetTimeWithOffset(t time.Time, offset time.Duration) time.Time

func GetWeekDay added in v0.2.30

func GetWeekDay(date time.Time, weekday time.Weekday) time.Time

获取这星期的星期几

func GetYearStart added in v0.2.31

func GetYearStart(t time.Time) time.Time

func HasReflectTypeInSlice

func HasReflectTypeInSlice(slice []reflect.Type, v interface{}) bool

func IsAndroid

func IsAndroid() bool

func IsBool added in v0.2.32

func IsBool(i interface{}) bool

func IsDarwin

func IsDarwin() bool

func IsEqual

func IsEqual(a interface{}, b interface{}) bool

func IsFileExist added in v0.0.6

func IsFileExist(path string) bool

func IsFileModified added in v0.2.4

func IsFileModified(filePath string) bool

func IsFileWithExt added in v0.1.24

func IsFileWithExt(p string, ext string) bool

func IsFreeBSD

func IsFreeBSD() bool

func IsIOS

func IsIOS() bool

func IsInt added in v0.2.32

func IsInt(i interface{}) bool

func IsLinux

func IsLinux() bool

func IsMac

func IsMac() bool

func IsNil added in v0.0.9

func IsNil(i interface{}) bool

func IsNilPointer

func IsNilPointer(c interface{}) bool

判断是否空指针

func IsOSX

func IsOSX() bool

func IsProcessExist added in v0.1.25

func IsProcessExist(s string) bool

func IsPublicIP added in v0.3.36

func IsPublicIP(IP net.IP) bool

func IsString added in v0.2.32

func IsString(i interface{}) bool

func IsToday

func IsToday(ts int64) bool

func IsWeb

func IsWeb() bool

func IsWindows

func IsWindows() bool

func LoadJson

func LoadJson(path string, v interface{})

func LoadRSAPrivateKeyPem

func LoadRSAPrivateKeyPem(p string) (*rsa.PrivateKey, error)

func LoadRSAPublicKeyPem

func LoadRSAPublicKeyPem(p string) (*rsa.PublicKey, error)

func MapToMap added in v0.3.36

func MapToMap(s interface{}) map[string]interface{}

func MilliSecondToString

func MilliSecondToString(millisec int64) string

func Nil added in v0.2.1

func Nil[T any]() T

func PathExists

func PathExists(path string) (bool, error)

func Recover added in v0.1.15

func Recover(r interface{}, all bool) error

func RemoveMapElement

func RemoveMapElement[T1 comparable, T2 comparable](obj map[T1]T2, elem T2) map[T1]T2

func RemoveMapWithCondition

func RemoveMapWithCondition[T1 comparable, T2 any](obj map[T1]T2, f func(key T1, elem T2) bool) map[T1]T2

func ReplaceTextParams added in v0.2.28

func ReplaceTextParams(text string, params ...interface{}) string

func SameDay

func SameDay(a, b time.Time) bool

func SizeStruct added in v0.3.36

func SizeStruct(data interface{}) int

func Sleep added in v0.1.43

func Sleep(ms int64)

func SleepUtil added in v0.1.43

func SleepUtil(ms int64, f func() bool)

func Struct2Map

func Struct2Map(s interface{}, fields ...string) (map[string]interface{}, error)

func Ternary

func Ternary[T any](expr bool, whenTrue, whenFalse T) T

func Time2TenYears

func Time2TenYears() time.Duration

得到当前时间到2028年的时间 一般用在排行榜上,比如同等级,判断时间先后

func TimeParseLocal

func TimeParseLocal(str string) (time.Time, error)

func ToMapInterface

func ToMapInterface(obj interface{}) map[interface{}]interface{}

func TryLock

func TryLock(redisAddr, lockKey string, ex uint, retry int) error

func USleep added in v0.1.43

func USleep(us int64)

func UnLock

func UnLock(redisAddr, lockKey string) error

func UnixNanoToString

func UnixNanoToString(nano int64) string

func UnixSecondToString

func UnixSecondToString(unix int64) string

func WaitForTerminate

func WaitForTerminate()

func WaitForTerminateChan

func WaitForTerminateChan(c chan struct{})

func WaitForTerminateChanCb

func WaitForTerminateChanCb(c chan struct{}, cb func())

func WalkDir

func WalkDir(dirPath string, recursive bool) ([]string, error)

func WalkDirDirs

func WalkDirDirs(dirPath string, recursive bool) ([]string, error)

func WalkDirDirsWithFunc

func WalkDirDirsWithFunc(dirPath string, walkFunc WalkDirFunc, recursive bool) ([]string, error)

func WalkDirFiles

func WalkDirFiles(dirPath string, recursive bool) ([]string, error)

func WalkDirFilesWithFunc

func WalkDirFilesWithFunc(dirPath string, walkFunc WalkDirFunc, recursive bool) ([]string, error)

func WalkDirWithFunc

func WalkDirWithFunc(dirPath string, walkFunc WalkDirFunc, recursive bool) ([]string, error)

Types

type Dispatcher

type Dispatcher interface {
}

type ID

type ID int64

An ID is a custom type used for a snowflake ID. This is used so we can attach methods onto the ID.

func IDFromB64

func IDFromB64(s string) ID

func IDFromString

func IDFromString(s string) ID

func ParseBase2

func ParseBase2(id string) (ID, error)

ParseBase2 converts a Base2 string into a snowflake ID

func ParseBase32

func ParseBase32(b []byte) (ID, error)

ParseBase32 parses a base32 []byte into a snowflake ID NOTE: There are many different base32 implementations so becareful when doing any interoperation.

func ParseBase36

func ParseBase36(id string) (ID, error)

ParseBase36 converts a Base36 string into a snowflake ID

func ParseBase58

func ParseBase58(b []byte) (ID, error)

ParseBase58 parses a base58 []byte into a snowflake ID

func ParseBase64

func ParseBase64(id string) (ID, error)

ParseBase64 converts a base64 string into a snowflake ID

func ParseBytes

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

ParseBytes converts a byte slice into a snowflake ID

func ParseInt64

func ParseInt64(id int64) ID

ParseInt64 converts an int64 into a snowflake ID

func ParseIntBytes

func ParseIntBytes(id [8]byte) ID

ParseIntBytes converts an array of bytes encoded as big endian integer as a snowflake ID

func ParseString

func ParseString(id string) (ID, error)

ParseString converts a string into a snowflake ID

func (ID) B64

func (this ID) B64() string

func (ID) Base2

func (f ID) Base2() string

Base2 returns a string base2 of the snowflake ID

func (ID) Base32

func (f ID) Base32() string

Base32 uses the z-base-32 character set but encodes and decodes similar to base58, allowing it to create an even smaller result string. NOTE: There are many different base32 implementations so becareful when doing any interoperation.

func (ID) Base36

func (f ID) Base36() string

Base36 returns a base36 string of the snowflake ID

func (ID) Base58

func (f ID) Base58() string

Base58 returns a base58 string of the snowflake ID

func (ID) Base64

func (f ID) Base64() string

Base64 returns a base64 string of the snowflake ID

func (ID) Bytes

func (f ID) Bytes() []byte

Bytes returns a byte slice of the snowflake ID

func (ID) Int64

func (f ID) Int64() int64

Int64 returns an int64 of the snowflake ID

func (ID) IntBytes

func (f ID) IntBytes() [8]byte

IntBytes returns an array of bytes of the snowflake ID, encoded as a big endian integer.

func (ID) IsValidProcessId

func (this ID) IsValidProcessId() bool

func (ID) MarshalJSON

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

MarshalJSON returns a json byte array string of the snowflake ID.

func (ID) Node

func (f ID) Node() int64

Snowflake returns an int64 of the snowflake ID node number DEPRECATED: the below function will be removed in a future release.

func (ID) ProcessId added in v0.1.24

func (this ID) ProcessId() ProcessId

func (ID) Step

func (f ID) Step() int64

Step returns an int64 of the snowflake step (or sequence) number DEPRECATED: the below function will be removed in a future release.

func (ID) String

func (f ID) String() string

String returns a string of the snowflake ID

func (ID) Time

func (f ID) Time() time.Time

func (ID) TimeSecond added in v0.1.30

func (f ID) TimeSecond() time.Time

func (ID) TimeUnix

func (f ID) TimeUnix() int64

Time returns an int64 unix timestamp in milliseconds of the snowflake ID time DEPRECATED: the below function will be removed in a future release.

func (*ID) UnmarshalJSON

func (f *ID) UnmarshalJSON(b []byte) error

UnmarshalJSON converts a json byte array of a snowflake ID into an ID type.

type Iterator

type Iterator struct {
	C <-chan interface{}
	// contains filtered or unexported fields
}

func (*Iterator) Stop

func (i *Iterator) Stop()

Stop stops the Iterator, no further elements will be received on C, C will be closed.

type JSONSyntaxError

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

A JSONSyntaxError is returned from UnmarshalJSON if an invalid ID is provided.

func (JSONSyntaxError) Error

func (j JSONSyntaxError) Error() string

type Job

type Job interface {
}

type OrderedPair

type OrderedPair struct {
	First  interface{}
	Second interface{}
}

An OrderedPair represents a 2-tuple of values.

func (*OrderedPair) Equal

func (pair *OrderedPair) Equal(other OrderedPair) bool

Equal says whether two 2-tuples contain the same values in the same order.

func (OrderedPair) String

func (pair OrderedPair) String() string

String outputs a 2-tuple in the form "(A, B)".

type PipeLine

type PipeLine interface {
}

type PipeWorker

type PipeWorker interface {
}

func CreatePipeWorker

func CreatePipeWorker() *PipeWorker

type ProcessId

type ProcessId uint16

func (ProcessId) Int32 added in v0.1.4

func (this ProcessId) Int32() int32

func (ProcessId) Int64 added in v0.1.4

func (this ProcessId) Int64() int64

func (ProcessId) IsValid

func (this ProcessId) IsValid() bool

func (ProcessId) Snowflake

func (this ProcessId) Snowflake() ID

func (ProcessId) ToString added in v0.1.52

func (this ProcessId) ToString() string

type Queue

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

func NewQueue

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

func (*Queue[T]) DeQueue

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

func (*Queue[T]) EnQueue

func (q *Queue[T]) EnQueue(item T)

func (Queue[T]) IsEmpty

func (q Queue[T]) IsEmpty() bool

func (Queue[T]) Size

func (q Queue[T]) Size() int

type RemoveMapCondition

type RemoveMapCondition func(key interface{}, elem interface{}) bool

type RingBuffer

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

func NewRingBuffer

func NewRingBuffer[T any](capacity int) *RingBuffer[T]

func (*RingBuffer[T]) Capacity

func (this *RingBuffer[T]) Capacity() int

func (*RingBuffer[T]) Deq

func (this *RingBuffer[T]) Deq() T

func (*RingBuffer[T]) DeqN

func (this *RingBuffer[T]) DeqN(count int) T

func (*RingBuffer[T]) Enq

func (this *RingBuffer[T]) Enq(element T) int

func (*RingBuffer[T]) IsEmpty

func (this *RingBuffer[T]) IsEmpty() bool

func (*RingBuffer[T]) IsFull

func (this *RingBuffer[T]) IsFull() bool

func (*RingBuffer[T]) Peek

func (this *RingBuffer[T]) Peek() T

func (*RingBuffer[T]) PeekN

func (this *RingBuffer[T]) PeekN(count int) []T

func (RingBuffer[T]) Size

func (this RingBuffer[T]) Size() int

type ScheduleTask

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

type Set

type Set interface {
	Add(i interface{}) bool

	Cardinality() int

	Clear()

	Clone() Set

	Contains(i ...interface{}) bool

	Difference(other Set) Set

	Equal(other Set) bool

	Intersect(other Set) Set

	IsProperSubset(other Set) bool

	IsProperSuperset(other Set) bool

	IsSubset(other Set) bool

	IsSuperset(other Set) bool

	Each(func(interface{}) bool)

	Iter() <-chan interface{}

	Iterator() *Iterator

	Remove(i interface{})

	String() string

	SymmetricDifference(other Set) Set

	Union(other Set) Set

	Pop() interface{}

	PowerSet() Set

	CartesianProduct(other Set) Set

	ToSlice() []interface{}
}

func NewSet

func NewSet(s ...interface{}) Set

func NewSetFromSlice

func NewSetFromSlice(s []interface{}) Set

func NewSetWith

func NewSetWith(elts ...interface{}) Set

func NewThreadUnsafeSet

func NewThreadUnsafeSet() Set

func NewThreadUnsafeSetFromSlice

func NewThreadUnsafeSetFromSlice(s []interface{}) Set

type Snowflake

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

A Snowflake struct holds the basic information needed for a snowflake generator node

func NewSnowflake

func NewSnowflake(node int64) (*Snowflake, error)

NewNode returns a new snowflake node that can be used to generate snowflake IDs

func (*Snowflake) Generate

func (n *Snowflake) Generate() ID

GenerateGoIds creates and returns a unique snowflake ID To help guarantee uniqueness - Make sure your system is keeping accurate system time - Make sure you never have multiple nodes running with the same node ID

type TaskID

type TaskID int

type Timer

type Timer struct {
	OnUpdate     func(dt int64, now int64)
	OnLateUpdate func(dt int64, now int64)
	OnStart      func(now int64)
	OnStop       func(now int64)
	OnPause      func(now int64)
	OnResume     func(now int64)
	OnDestroy    func(now int64)
	// contains filtered or unexported fields
}

func CreateTimer

func CreateTimer(updateTime int64, timeScale float32, sign chan<- int) *Timer

func (*Timer) GetPrevInterval

func (t *Timer) GetPrevInterval() int64

func (*Timer) GetRunningTime

func (t *Timer) GetRunningTime() int64

func (*Timer) GetTimeOffset

func (t *Timer) GetTimeOffset() int64

func (*Timer) GetTimeScale

func (t *Timer) GetTimeScale() float32

func (*Timer) GetUpdateTime

func (t *Timer) GetUpdateTime() int64

func (*Timer) Now

func (t *Timer) Now() int64

func (*Timer) Pause

func (t *Timer) Pause()

func (*Timer) Reset

func (t *Timer) Reset()

func (*Timer) Resume

func (t *Timer) Resume()

func (*Timer) Schedule

func (t *Timer) Schedule(name string, interval int64, count int, task func(int64, int64), delay int64, startTime int64)

func (*Timer) Start

func (t *Timer) Start()

func (*Timer) Stop

func (t *Timer) Stop()

func (*Timer) Unschedule

func (t *Timer) Unschedule(task interface{})

type TimerState

type TimerState int
const (
	TIMER_STOP   TimerState = 0
	TIMER_START  TimerState = 1
	TIMER_ONSTOP TimerState = 2
)

type TimerType

type TimerType int
const (
	TIMER_SYNC  TimerType = 0
	TIMER_ASYNC TimerType = 1
	TIMER_FIXED TimerType = 2
)

type WalkDirFunc

type WalkDirFunc func(filePath string, file os.FileInfo) bool

type WorkerPool

type WorkerPool interface {
	IsEmpty() bool
	IsClosed() bool
	AddJob(job *Job)
}

Directories

Path Synopsis
this is a generate file,do not modify it!
this is a generate file,do not modify it!

Jump to

Keyboard shortcuts

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