utils

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2023 License: Apache-2.0 Imports: 28 Imported by: 0

Documentation

Index

Constants

View Source
const (
	BYTE = 1 << (10 * iota)
	KILOBYTE
	MEGABYTE
	GIGABYTE
	TERABYTE
	PETABYTE
	EXABYTE
)
View Source
const (
	TimeOffset = 8 * 3600  //8 hour offset
	HalfOffset = 12 * 3600 //Half-day hourly offset
)

Variables

View Source
var ServerIP = ""

Functions

func AesDecrypt

func AesDecrypt(data []byte, key []byte) ([]byte, error)

func AesEncrypt

func AesEncrypt(data []byte, key []byte) ([]byte, error)

func Base64Decode

func Base64Decode(data string) string

func Base64Encode

func Base64Encode(data string) string

func Batch

func Batch[T any, V any](fn func(T) V, ts []T) []V

func BothExist

func BothExist[E comparable](es ...[]E) []E

BothExist 获取切片中共同存在的元素(交集)

func BothExistAny

func BothExistAny[E any, K comparable](es [][]E, fn func(e E) K) []E

BothExistAny 获取切片中共同存在的元素(交集)

func ByteSize

func ByteSize(bytes uint64) string

func Complete

func Complete[E comparable](a []E, b []E) bool

Complete a和b去重后是否相等(忽略顺序)

func Contain

func Contain[E comparable](e E, es ...E) bool

Contain 是否包含

func CopyStructFields

func CopyStructFields(a interface{}, b interface{}, fields ...string) (err error)

copy a by b b->a

func Delete

func Delete[E any](es []E, index ...int) []E

Delete 删除切片元素, 支持负数删除倒数第几个

func DeleteAt

func DeleteAt[E any](es *[]E, index ...int) []E

DeleteAt 删除切片元素, 支持负数删除倒数第几个

func Difference

func Difference(slice1, slice2 []int64) []int64

Get the diff of two slices

func DifferenceString

func DifferenceString(slice1, slice2 []string) []string

Get the diff of two slices

func Distinct

func Distinct[T comparable](ts []T) []T

Distinct 去重

func DistinctAny

func DistinctAny[E any, K comparable](es []E, fn func(e E) K) []E

DistinctAny 去重

func DistinctAnyGetComparable

func DistinctAnyGetComparable[E any, K comparable](es []E, fn func(e E) K) []K

func Duplicate

func Duplicate[E comparable](es []E) bool

Duplicate 是否有重复的

func DuplicateAny

func DuplicateAny[E any, K comparable](es []E, fn func(e E) K) bool

DuplicateAny 是否有重复的

func Equal

func Equal[E comparable](a []E, b []E) bool

Equal 比较切片是否相对(包括元素顺序)

func Filter

func Filter[E, T any](es []E, fn func(e E) (T, bool)) []T

func GenConversationIDForSingle

func GenConversationIDForSingle(sendID, recvID string) string

func GenConversationUniqueKeyForGroup

func GenConversationUniqueKeyForGroup(groupID string) string

func GenConversationUniqueKeyForSingle

func GenConversationUniqueKeyForSingle(sendID, recvID string) string

func GenGroupConversationID

func GenGroupConversationID(groupID string) string

func GenID

func GenID() string

func GenIDs

func GenIDs(count int) []string

func GenSmallImage

func GenSmallImage(src, dst string) error

func GetCurDayHalfTimeFormat

func GetCurDayHalfTimeFormat() string

Get the formatted time at 12 o'clock of the day, the format is "2006-01-02_12-00-00"

func GetCurDayHalfTimestamp

func GetCurDayHalfTimestamp() int64

Get the timestamp at 12 o'clock on the day

func GetCurDayZeroTimeFormat

func GetCurDayZeroTimeFormat() string

Get the formatted time at 0 o'clock of the day, the format is "2006-01-02_00-00-00"

func GetCurDayZeroTimestamp

func GetCurDayZeroTimestamp() int64

Get the timestamp at 0 o'clock of the day

func GetCurrentTimestampByMill

func GetCurrentTimestampByMill() int64

Get the current timestamp by Mill

func GetCurrentTimestampByNano

func GetCurrentTimestampByNano() int64

Get the current timestamp by Nano

func GetCurrentTimestampBySecond

func GetCurrentTimestampBySecond() int64

Get the current timestamp by Second

func GetFuncName

func GetFuncName(skips ...int) string

func GetHashCode

func GetHashCode(s string) uint32

func GetLocalIP

func GetLocalIP() (string, error)

func GetMsgID

func GetMsgID(sendID string) string

func GetNotificationConversationIDByConversationID

func GetNotificationConversationIDByConversationID(conversationID string) string

func GetSelfFuncName

func GetSelfFuncName() string

func GetSelfNotificationConversationID

func GetSelfNotificationConversationID(userID string) string

func GetSeqsBeginEnd

func GetSeqsBeginEnd(seqs []int64) (int64, int64)

func GetSwitchFromOptions

func GetSwitchFromOptions(Options map[string]bool, key string) (result bool)

func GetTimeStampByFormat

func GetTimeStampByFormat(datetime string) string

func HasKey

func HasKey[K comparable, V any](m map[K]V, k K) bool

HasKey get whether the map contains key

func If

func If[T any](isa bool, a, b T) T

If true -> a, false -> b

func IndexAny

func IndexAny[E any, K comparable](e E, es []E, fn func(e E) K) int

IndexAny get the index of the element

func IndexOf

func IndexOf[E comparable](e E, es ...E) int

IndexOf get the index of the element

func InitMap

func InitMap[K comparable, V any](val *map[K]V)

func InitSlice

func InitSlice[T any](val *[]T)

func Int32ToString

func Int32ToString(i int32) string

func Int64ToString

func Int64ToString(i int64) string

func IntToString

func IntToString(i int) string

func InterfaceArrayToStringArray

func InterfaceArrayToStringArray(data []interface{}) (i []string)

func Intersect

func Intersect(slice1, slice2 []int64) []int64

Get the intersection of two slices

func IntersectString

func IntersectString(slice1, slice2 []string) []string

Get the intersection of two slices

func IsContain

func IsContain(target string, List []string) bool

judge a string whether in the string list

func IsContainInt

func IsContainInt(target int, List []int) bool

func IsContainInt32

func IsContainInt32(target int32, List []int32) bool

func IsDir

func IsDir(path string) bool

Determine whether the given path is a folder

func IsDuplicateStringSlice

func IsDuplicateStringSlice(arr []string) bool

func IsFile

func IsFile(path string) bool

Determine whether the given path is a file

func JsonStringToMap

func JsonStringToMap(str string) (tempMap map[string]int32)

func JsonStringToStruct

func JsonStringToStruct(s string, args interface{}) error

The incoming parameter must be a pointer

func Keys

func Keys[K comparable, V any](kv map[K]V) []K

Keys get map keys

func MapIntToJsonString

func MapIntToJsonString(param map[string]int32) string

func MapToJsonString

func MapToJsonString(param map[string]interface{}) string

func Max

func Max[E Ordered](e ...E) E

Max get maximum value

func Md5

func Md5(s string, salt ...string) string

func Min

func Min[E Ordered](e ...E) E

Min get minimum value

func MkDir

func MkDir(path string) error

Create a directory

func NotNilReplace

func NotNilReplace[T any](old, new_ *T)

NotNilReplace 当new_不为空时, 将old设置为new_

func OperationIDGenerator

func OperationIDGenerator() string

func Order

func Order[E comparable, T any](es []E, ts []T, fn func(t T) E) []T

Order 将ts按es排序

func OrderPtr

func OrderPtr[E comparable, T any](es []E, ts *[]T, fn func(t T) E) []T

func Paginate

func Paginate[E any](es []E, pageNumber int, showNumber int) []E

func RemoveDuplicate

func RemoveDuplicate[T comparable](arr []T) []T

func RemoveDuplicateElement

func RemoveDuplicateElement(idList []string) []string

func SetSwitchFromOptions

func SetSwitchFromOptions(options map[string]bool, key string, value bool)

func Single

func Single[E comparable](a, b []E) []E

Single a中存在,b中不存在 或 b中存在,a中不存在

func Slice

func Slice[E any, T any](es []E, fn func(e E) T) []T

Slice 批量转换切片类型

func SliceAnySub

func SliceAnySub[E any, T comparable](a, b []E, fn func(t E) T) []E

SliceAnySub a中存在,b中不存在 (a-b) fn 返回的是uuid

func SliceSet

func SliceSet[E comparable](es []E) map[E]struct{}

SliceSet slice to map[E]struct{}

func SliceSetAny

func SliceSetAny[E any, K comparable](es []E, fn func(e E) K) map[K]struct{}

SliceSetAny slice to map[K]struct{}

func SliceSub

func SliceSub[E comparable](a, b []E) []E

SliceSub a中存在,b中不存在 (a-b)

func SliceSubAny

func SliceSubAny[E comparable, T any](a []E, b []T, fn func(t T) E) []E

SliceSubAny a中存在,b中不存在 (a-b)

func SliceToMap

func SliceToMap[E any, K comparable](es []E, fn func(e E) K) map[K]E

SliceToMap slice to map

func SliceToMapAny

func SliceToMapAny[E any, K comparable, V any](es []E, fn func(e E) (K, V)) map[K]V

SliceToMapAny slice to map (自定义类型)

func SliceToMapOkAny

func SliceToMapOkAny[E any, K comparable, V any](es []E, fn func(e E) (K, V, bool)) map[K]V

SliceToMapOkAny slice to map (自定义类型, 筛选)

func Sort

func Sort[E Ordered](es []E, asc bool) []E

Sort basic type sorting

func SortAny

func SortAny[E any](es []E, fn func(a, b E) bool)

SortAny custom sort method

func StringToInt

func StringToInt(i string) int

func StringToInt32

func StringToInt32(i string) int32

func StringToInt64

func StringToInt64(i string) int64

func StructToJsonBytes

func StructToJsonBytes(param interface{}) []byte

func StructToJsonString

func StructToJsonString(param interface{}) string

func TimeStringFormatTimeUnix

func TimeStringFormatTimeUnix(timeFormat string, timeSrc string) int64

func TimeStringToTime

func TimeStringToTime(timeString string) (time.Time, error)

func TimeToString

func TimeToString(t time.Time) string

func ToPtr

func ToPtr[T any](t T) *T

func Uint32ToString

func Uint32ToString(i uint32) string

func UniqueJoin

func UniqueJoin(s ...string) string

func UnixMillSecondToTime

func UnixMillSecondToTime(millSecond int64) time.Time

func UnixNanoSecondToTime

func UnixNanoSecondToTime(nanoSecond int64) time.Time

Convert nano timestamp to time.Time type

func UnixSecondToTime

func UnixSecondToTime(second int64) time.Time

Convert timestamp to time.Time type

func Unwrap

func Unwrap(err error) error

func Values

func Values[K comparable, V any](kv map[K]V) []V

Values get map values

func WithMessage

func WithMessage(err error, message string) error

func Wrap

func Wrap(err error, message string) error

func Wrap1

func Wrap1(err error) error

func Wrap2

func Wrap2[T any](a T, err error) (T, error)

func Wrap3

func Wrap3[T any, V any](a T, b V, err error) (T, V, error)

Types

type Map

type Map struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func (*Map) Del

func (m *Map) Del(key interface{})

func (*Map) Get

func (m *Map) Get(key interface{}) interface{}

func (*Map) Len

func (m *Map) Len() int

func (*Map) LockRange

func (m *Map) LockRange(f func(interface{}, interface{}))

func (*Map) RLockRange

func (m *Map) RLockRange(f func(interface{}, interface{}))

func (*Map) Set

func (m *Map) Set(key interface{}, value interface{})

func (*Map) TestAndSet

func (m *Map) TestAndSet(key interface{}, value interface{}) interface{}

func (*Map) UnsafeDel

func (m *Map) UnsafeDel(key interface{})

func (*Map) UnsafeGet

func (m *Map) UnsafeGet(key interface{}) interface{}

func (*Map) UnsafeLen

func (m *Map) UnsafeLen() int

func (*Map) UnsafeRange

func (m *Map) UnsafeRange(f func(interface{}, interface{}))

func (*Map) UnsafeSet

func (m *Map) UnsafeSet(key interface{}, value interface{})

type Ordered

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

Ordered types that can be sorted

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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