utility

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2024 License: MIT Imports: 18 Imported by: 0

README

go-utility

Universal Utilities for Go

Documentation

Index

Constants

View Source
const (
	CNGMT = "+0800"
	// Time duration for one day.
	TimeDay = 24 * time.Hour
)
View Source
const (
	MaxSnowflakeNodeIndex = 1023
)
View Source
const TimeLayout = "2006-01-02 15:04:05"

Variables

View Source
var (
	CNTimeLocation *time.Location
)

Functions

func AnyArrayToFloat64Array

func AnyArrayToFloat64Array(arrInterface []interface{}) []float64

func AnyArrayToInt64Array

func AnyArrayToInt64Array(arrInterface []interface{}) []int64

func AnyArrayToStringArray

func AnyArrayToStringArray(arrInterface []interface{}) []string

func AnyToBool

func AnyToBool(v interface{}) bool

func AnyToBoolArray

func AnyToBoolArray(any interface{}) []bool

func AnyToFloat64

func AnyToFloat64(value interface{}) float64

func AnyToFloat64Array

func AnyToFloat64Array(any interface{}) []float64

func AnyToInt64

func AnyToInt64(value interface{}) int64

func AnyToInt64Array

func AnyToInt64Array(any interface{}) []int64

func AnyToMapStringAny

func AnyToMapStringAny(content any) map[string]any

AnyToMapStringAny 将 Content 转化为 Map<String>Any,使其序列化时按字典序排序

func AnyToString

func AnyToString(value interface{}) string

func AnyToStringArray

func AnyToStringArray(any interface{}) []string

func BytesToString

func BytesToString(b []byte) string

BytesToString 按string的底层结构,转换[]byte

func CNFormatToTime

func CNFormatToTime(timeString string) time.Time

func CanConvertToFloat32Loselessly

func CanConvertToFloat32Loselessly(v float64) bool

func CanConvertToInt32Loselessly

func CanConvertToInt32Loselessly(v float64) bool

func CanConvertToInt64Loselessly

func CanConvertToInt64Loselessly(v float64) bool

func CheckEmail

func CheckEmail(str string) bool

func DayDiff

func DayDiff(date1, date2 time.Time, loc *time.Location) int

func EnvironmentVariables

func EnvironmentVariables() map[string]string

func FilterSlice

func FilterSlice[Element any](slice []Element, fn func(e Element) bool) (ret []Element)

func FindFirstInSliceWhere

func FindFirstInSliceWhere[Element any](slice []Element, fn func(e Element) bool) int

func FindFirstRefInSliceWhere

func FindFirstRefInSliceWhere[Element any](slice []Element, fn func(e Element) bool) *Element

func FindInAnyMap

func FindInAnyMap(m AnyMap, keys ...interface{}) interface{}

func FindInAnyMapWithKeys

func FindInAnyMapWithKeys(m AnyMap, keys []interface{}) interface{}

func FindInFloat64Array

func FindInFloat64Array(slice []float64, item float64) int

func FindInInt64Array

func FindInInt64Array(slice []int64, item int64) int

func FindInSlice

func FindInSlice[Element comparable](slice []Element, item Element) int

FindInStrings would return first index of `slice` if found `item`, or -1 for not.

func FindInStrMap

func FindInStrMap(m StrMap, keys ...interface{}) interface{}

func FindInStrMapWithKeys

func FindInStrMapWithKeys(m StrMap, keys []interface{}) interface{}

func FindInStrings

func FindInStrings(slice []string, item string) int

FindInStrings would return first index of `slice` if found `item`, or -1 for not.

func FindInSyncMap

func FindInSyncMap(m *sync.Map, keys ...interface{}) interface{}

func FindInSyncMapWithKeys

func FindInSyncMapWithKeys(m *sync.Map, keys []interface{}) interface{}

func GenID

func GenID(prefix string) string

func GenPassword

func GenPassword() string

GenPassword 生成随机密码,32位长度,只包含小写英文及数字,开头不是数字

func GenUUID

func GenUUID() string

func GenerateFixedLengthRandomString

func GenerateFixedLengthRandomString(length uint8) string

func GenerateFixedLengthUUID

func GenerateFixedLengthUUID(length uint8) string

GenerateFixedLengthUUID generates base32 uuid with the given length. the generated uuid consists of two parts:`timestampBase32String` and `randomBase32String` `timestampBase32String` is generated by base32-encoded millisecond timestamp, length is 8. `randomBase32String` is generated by base32-encoded random int64 which is generated by `math/rand` library. parameter `length` should be at least 8 if uniqueness need to be guaranteed in one millisecond.

func GenerateSnowflakeID

func GenerateSnowflakeID() snowflake.ID

GenerateSnowflakeID 产生随机ID.

func GetCNCurrentTime

func GetCNCurrentTime() time.Time

func GetCNFormatCurrentTime

func GetCNFormatCurrentTime() string

func Hash

func Hash(bs []byte) uint32

func InitSnowflakeNode

func InitSnowflakeNode(index int64)

InitSnowflakeNode 使用index初始化随机ID生成器,index需在[0,1023]之间.

func IsAlnumString

func IsAlnumString(s string) bool

func IsAlphaString

func IsAlphaString(s string) bool

func IsDateEqual

func IsDateEqual(date1, date2 time.Time, loc *time.Location) bool

func IsNumString

func IsNumString(s string) bool

func IsNumeric

func IsNumeric(value interface{}) bool

func IsSliceEqual

func IsSliceEqual[Element comparable](slice1, slice2 []Element) bool

func MapSlice

func MapSlice[Element any, Return any](slice []Element, fn func(Element) Return) []Return

func Max

func Max[Value Number](l, r Value) Value

func MaxInt64

func MaxInt64(a, b int64) int64

func MillisecondOfTime

func MillisecondOfTime(t time.Time) int64

func Min

func Min[Value Number](l, r Value) Value

func MinInt64

func MinInt64(a, b int64) int64

func PanicIfNotNil

func PanicIfNotNil(err error)

func RemovePrefixKeys

func RemovePrefixKeys(data map[string]any, prefix string)

RemovePrefixKeys 删除 Map 中指定前缀的 Key

func ReversedSlice

func ReversedSlice[Element any](slice []Element) []Element

func StringPToBytes

func StringPToBytes(s *string) []byte

func StringToBytes

func StringToBytes(s string) []byte

StringToBytes 按[]byte的底层结构,转换字符串,len与cap皆为字符串的len

func StringToChunks

func StringToChunks(s string, chunkSize int) []string

StringToChunks split a string into string slices with element's size <= chunkSize Examples: StringToChunks("abcd", 1) => []string{"a", "b", "c", "d"} StringToChunks("abcd", 2) => []string{"ab", "cd"} StringToChunks("abcd", 3) => []string{"abc", "d"} stringToChunks("abcd", 4) => []string{"abcd"} stringToChunks("abcd", 5) => []string{"abcd"}

func StringToInt64

func StringToInt64(value string) (int64, error)

func StringWithCharset

func StringWithCharset(length int, charset string) string

func TimeInSnowflakeID

func TimeInSnowflakeID(id snowflake.ID) time.Time

func TimeInSnowflakeInt

func TimeInSnowflakeInt(id int64) time.Time

func TimeToCNFormat

func TimeToCNFormat(t time.Time) string

Types

type AnyMap

type AnyMap = map[interface{}]interface{}

func AnyToAnyMap

func AnyToAnyMap(value interface{}) AnyMap

type CustomStringConvertable

type CustomStringConvertable interface {
	String() string
}

type Number

type Number interface {
	~int8 | ~int16 | ~int32 | ~int64 | ~int |
		~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uint |
		~float32 | ~float64
}

type Optional

type Optional[Wrapped any] struct {
	// contains filtered or unexported fields
}

Optional is a type represents either a wrapped value or `nil`, the absence of a value.

func FlatMapOptional

func FlatMapOptional[Wrapped any, Return any](opt Optional[Wrapped], fn func(Wrapped) Optional[Return]) Optional[Return]

func MapOptional

func MapOptional[Wrapped any, Return any](opt Optional[Wrapped], fn func(Wrapped) Return) Optional[Return]

func NoneOptional

func NoneOptional[Wrapped any]() Optional[Wrapped]

func OptionalWithPointer

func OptionalWithPointer[Wrapped any](v *Wrapped) Optional[Wrapped]

Wrap nullable pointer as Optional, nil with NoneOptional, non-nil with SomeOptional.

func SomeOptional

func SomeOptional[Wrapped any](v Wrapped) Optional[Wrapped]

func (Optional[Wrapped]) Get

func (this Optional[Wrapped]) Get() Wrapped

Get value if `valid` or zero value of Wrapped.

func (Optional[Wrapped]) IsValid

func (this Optional[Wrapped]) IsValid() bool

IsValid of the value.

type RWLock

type RWLock[Value any] struct {
	// contains filtered or unexported fields
}

func NewRWLock

func NewRWLock[Value any](v Value) RWLock[Value]

func (*RWLock[Value]) Read

func (self *RWLock[Value]) Read() Value

func (*RWLock[Value]) ReadFn

func (self *RWLock[Value]) ReadFn(op func(v *Value))

func (*RWLock[Value]) Write

func (self *RWLock[Value]) Write(v Value)

func (*RWLock[Value]) WriteFn

func (self *RWLock[Value]) WriteFn(op func(v *Value))

type Range

type Range[Bound Number] struct {
	// contains filtered or unexported fields
}

Range of any number `Bound`.

func NewClosedRange

func NewClosedRange[Bound Number](lower, upper Bound) Range[Bound]

Range with [lower, upper].

func NewUpperOpenedRange

func NewUpperOpenedRange[Bound Number](lower, upper Bound) Range[Bound]

Range with [lower, upper).

func (Range[Bound]) Contains

func (this Range[Bound]) Contains(v Bound) bool

func (Range[Bound]) IsEmpty

func (this Range[Bound]) IsEmpty() bool

func (Range[Bound]) WithLowerOpened

func (this Range[Bound]) WithLowerOpened() Range[Bound]

func (Range[Bound]) WithUpperOpened

func (this Range[Bound]) WithUpperOpened() Range[Bound]

type Result

type Result[Success any, Failure error] struct {
	// contains filtered or unexported fields
}

A value that represents either a success or a failure, including an associated value in each case.

func FlatMapResult

func FlatMapResult[Success any, Failure error, NewSuccess any](
	result Result[Success, Failure],
	fn func(Success) Result[NewSuccess, Failure],
) Result[NewSuccess, Failure]

func MapResult

func MapResult[Success any, Failure error, NewSuccess any](
	result Result[Success, Failure],
	fn func(Success) NewSuccess,
) Result[NewSuccess, Failure]

func ResultWithFailure

func ResultWithFailure[Success any, Failure error](e Failure) Result[Success, Failure]

func ResultWithSuccess

func ResultWithSuccess[Success any, Failure error](v Success) Result[Success, Failure]

func (Result[Success, Failure]) Failure

func (this Result[Success, Failure]) Failure() Optional[Failure]

func (Result[Success, Failure]) IsSuccess

func (this Result[Success, Failure]) IsSuccess() bool

func (Result[Success, Failure]) Success

func (this Result[Success, Failure]) Success() Optional[Success]

type Set

type Set[Element comparable] struct {
	// contains filtered or unexported fields
}

An unordered collection of unique elements.

func NewSet

func NewSet[Element comparable](values ...Element) Set[Element]

func (*Set[Element]) Add

func (this *Set[Element]) Add(values ...Element)

func (Set[Element]) All

func (this Set[Element]) All() []Element

func (Set[Element]) Contains

func (this Set[Element]) Contains(value Element) bool

func (*Set[Element]) Count

func (this *Set[Element]) Count() int

func (*Set[Element]) Remove

func (this *Set[Element]) Remove(values ...Element)

type StrMap

type StrMap = map[string]interface{}

func AnyArrayToStrMap

func AnyArrayToStrMap(mapInterface []interface{}) StrMap

func AnyToStrMap

func AnyToStrMap(value interface{}) StrMap

func FlattenMap

func FlattenMap(rootKey, delimiter string, originData StrMap) StrMap

flatten map ,e.g map A

{
	"foo":{
		"bar":1
	}
}

map B = FlattenMap("",".",A)

{
	"foo.bar":1
}

type TimeInDay

type TimeInDay struct {
	Hour   int
	Minute int
	Second int
}

func MakeTimeInDay

func MakeTimeInDay(hour, minute, second int) TimeInDay

func ParseTimeInDay

func ParseTimeInDay(t string) (inst TimeInDay)

ParseTimeInDay would parse TimeInDay from time string, e.g. 09 or 8:25 or 22:03:2.

func (TimeInDay) Compare

func (t TimeInDay) Compare(other time.Time) int8

Compare with the other time in the day of the other time.

  • Return: receiver -1 early, 0 same, 1 later than the other time

func (TimeInDay) IsValid

func (t TimeInDay) IsValid() bool

func (TimeInDay) TimeOnDay

func (t TimeInDay) TimeOnDay(day time.Time) time.Time

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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