utils

package
v0.0.0-...-e14b3c0 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2024 License: MIT Imports: 39 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Linux   = runtime.GOOS == "linux"
	Windows = runtime.GOOS == "windows"
)

Variables

Functions

func Abs

func Abs[T NumberType](i T) T

func BackoffDelay

func BackoffDelay(attempts int) time.Duration

func CRC32Sum

func CRC32Sum(value []byte) string

func CopyFile

func CopyFile(src, dst string) (err error)

func CopySlice

func CopySlice[T any](src []T) []T

CopySlice 拷贝列表

func DJB33

func DJB33(seed uint32, k string) uint32

func DownloadToBuffer

func DownloadToBuffer(url string, writer io.Writer) error

func DownloadToFile

func DownloadToFile(url, path string) (err error)

DownloadToFile 下载文件

func DownloadToFolder

func DownloadToFolder(url, folder string) (err error)

DownloadToFolder 下载文件到目录

func ExpireTime

func ExpireTime(timeout int64) (int64, int64)

func FromBase62

func FromBase62(s string) (n int, err error)

func GetField

func GetField(object any, key string) (any, error)

func GetHostAddr

func GetHostAddr(host string) (string, error)

func GetHostTLD

func GetHostTLD(host string) (string, error)

func GetStringsByField

func GetStringsByField[T any](object []T, key string) ([]string, error)

func HashString

func HashString(mode HashMode, s ...string) string

func HostPort

func HostPort(addr string, port interface{}) string

HostPort format addr and port suitable for dial.

func IPs

func IPs() []string

IPs returns all available interface IP addresses.

func IncludeInSlice

func IncludeInSlice[T comparable](s []T, target T) bool

IncludeInSlice 判断包含

func InsertSlice

func InsertSlice[T any](s []T, value T) []T

InsertSlice 插入到最开始

func IntSliceToString

func IntSliceToString[T IntType](s []T) []string

IntSliceToString int类型转换string

func IntToString

func IntToString[T IntType](value T) string

func IsLocal

func IsLocal(addr string) bool

IsLocal checks whether an IP belongs to one of the device's interfaces.

func IsSafePathName

func IsSafePathName(name string) bool

func IsSafeUnixLikePath

func IsSafeUnixLikePath(path string) bool

func JsonConvert

func JsonConvert(a, b any) error

func Listen

func Listen(addr string) (net.Listener, error)

func LoadBuff

func LoadBuff(path string) ([]byte, error)

func LoadJson

func LoadJson(path string, payload any) error

func MD5Sum

func MD5Sum(value []byte) string

func MakeDirs

func MakeDirs(path string, strict ...bool) error

func MapKeyToSlice

func MapKeyToSlice[T1 comparable, T2 any](m map[T1]T2) []T1

MapKeyToSlice map key转换成列表

func MapValueToSlice

func MapValueToSlice[T1 comparable, T2 any](m map[T1]T2) []T2

MapValueToSlice map 值转换成列表

func Md5Hmac

func Md5Hmac(value, salt string) string

func MergeSlice

func MergeSlice[T any](slices ...[]T) []T

MergeSlice 列表合并

func Monotonic

func Monotonic() time.Duration

func NowUnix

func NowUnix() int64

func OrderedQuery

func OrderedQuery(values *OrderedMap[string, string]) string

OrderedQuery 按照字典写入顺的生成query string

func ParseQuery

func ParseQuery(query string) (values map[string]string, err error)

ParseQuery query string转化为map

func PathExist

func PathExist(path string) (bool, error)

func PathFileInfo

func PathFileInfo(path string) (os.FileInfo, error)

func PathIsDir

func PathIsDir(path string) (bool, error)

func PathIsRegularFile

func PathIsRegularFile(path string) (bool, error)

func PathIsUnixSockFile

func PathIsUnixSockFile(path string) (bool, error)

func PopQuery

func PopQuery(query string, skip ...string) (string, error)

func RandString

func RandString(n int) string

RandString 返回一个指定长度的随机字符串

func RandomHex

func RandomHex(length int) string

func RandomInt

func RandomInt(n int) int

func ReadFile

func ReadFile(path string) ([]byte, error)

func RemoveElementSlice

func RemoveElementSlice[T comparable](sp *[]T, target T)

RemoveElementSlice 从列表中剔除元素(不创建新列表)

func SaveBuff

func SaveBuff(path string, payload []byte, overwrite ...bool) error

func SaveJson

func SaveJson(path string, payload any, overwrite ...bool) error

func Sha1Hmac

func Sha1Hmac(value, salt string) string

func Sha1Sum

func Sha1Sum(value []byte) string

func Sha256Hmac

func Sha256Hmac(value, salt string) string

func Sha256Sum

func Sha256Sum(value []byte) string

func SliceReverse

func SliceReverse[T any](s []T) []T

func SliceToMap

func SliceToMap[T any, K comparable](s []T, keyFunc func(T) K) map[K]T

func SliceToMapByField

func SliceToMapByField[T any, K comparable](s []T, field ...string) (map[K]T, error)

func SliceToMapByStringField

func SliceToMapByStringField[T any](s []T, key ...string) (map[string]T, error)

func SortSlice

func SortSlice[T SortAbleType](src []T, reverse ...bool) []T

SortSlice 排序

func SortedQuery

func SortedQuery(values map[string]string) string

SortedQuery 按照key升序的顺序生成的query string

func StrToI32

func StrToI32(value string) (int32, error)

func StringSliceToInt

func StringSliceToInt[T IntType](s []string) ([]T, error)

StringSliceToInt string 转换未int

func StringToInt

func StringToInt(value string) (int, error)

func StringToMoney

func StringToMoney(s string) (value decimal.Decimal, err error)

func Supported

func Supported(supports []string, target string) bool

func ToBase62

func ToBase62(n int) string

func ToBase62Bytes

func ToBase62Bytes(n int) (s []byte)

func ToString

func ToString(i any) (string, error)

ToString casts any type to a string type.

func TopLevelDomain

func TopLevelDomain(domain string) (string, error)

TopLevelDomain 获取顶级域名

func UnSafeGetStringsByField

func UnSafeGetStringsByField[T any](object []T, key string) []string

func UnZip

func UnZip(source, output string) error

UnZip 解压到文件夹

func UnsafeStrToI32

func UnsafeStrToI32(value string) int32

func UnsafeToString

func UnsafeToString(i any) string

func VerifyAddr

func VerifyAddr(listen string) bool

func VerifyIP

func VerifyIP(addr string) (string, error)

VerifyIP (Extract) returns a valid IP address. If the address provided is a valid address, it will be returned directly. Otherwise the available interfaces be itterated over to find an IP address, prefferably private.

func Zip

func Zip(source, output string) error

Zip 压缩指定文件、文件夹

func ZipCompress

func ZipCompress(data []byte) ([]byte, error)

func ZipDecompress

func ZipDecompress(data []byte) ([]byte, error)

func ZipFileBuff

func ZipFileBuff(name string, data []byte) ([]byte, error)

Types

type BaseType

type BaseType interface {
	~int | ~int8 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~float32 | ~float64 | ~bool | ~string | ~complex64 | ~complex128
}

type Element

type Element[K comparable, V any] struct {

	// The key that corresponds to this element in the ordered map.
	Key K

	// The value stored with this element.
	Value V
	// contains filtered or unexported fields
}

Element is an element of a null terminated (non circular) intrusive doubly linked list that contains the key of the correspondent element in the ordered map too.

func (*Element[K, V]) Next

func (e *Element[K, V]) Next() *Element[K, V]

Next returns the next list element or nil.

func (*Element[K, V]) Prev

func (e *Element[K, V]) Prev() *Element[K, V]

Prev returns the previous list element or nil.

type HashMode

type HashMode string
const (
	MD5    HashMode = "md5"
	SHA1   HashMode = "sha1"
	SHA256 HashMode = "sha256"
	CRC32  HashMode = "crc32"
)

type IntType

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

type KeyLocks

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

func NewKeyLocks

func NewKeyLocks() *KeyLocks

func (*KeyLocks) Get

func (m *KeyLocks) Get(key string) *sync.RWMutex

func (*KeyLocks) Lock

func (m *KeyLocks) Lock(key string)

func (*KeyLocks) RLock

func (m *KeyLocks) RLock(key string)

func (*KeyLocks) UnLock

func (m *KeyLocks) UnLock(key string)

func (*KeyLocks) UnRLock

func (m *KeyLocks) UnRLock(key string)

type NumberType

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

type OrderedMap

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

func NewOrderedMap

func NewOrderedMap[K comparable, V any]() *OrderedMap[K, V]

func (*OrderedMap[K, V]) Back

func (m *OrderedMap[K, V]) Back() *Element[K, V]

Back will return the element that is the last (most recent Set element). If there are no elements this will return nil.

func (*OrderedMap[K, V]) Copy

func (m *OrderedMap[K, V]) Copy() *OrderedMap[K, V]

Copy returns a new OrderedMap with the same elements. Using Copy while there are concurrent writes may mangle the result.

func (*OrderedMap[K, V]) Delete

func (m *OrderedMap[K, V]) Delete(key K) (didDelete bool)

Delete will remove a key from the map. It will return true if the key was removed (the key did exist).

func (*OrderedMap[K, V]) Front

func (m *OrderedMap[K, V]) Front() *Element[K, V]

Front will return the element that is the first (oldest Set element). If there are no elements this will return nil.

func (*OrderedMap[K, V]) Get

func (m *OrderedMap[K, V]) Get(key K) (value V, ok bool)

Get returns the value for a key. If the key does not exist, the second return parameter will be false and the value will be nil.

func (*OrderedMap[K, V]) GetElement

func (m *OrderedMap[K, V]) GetElement(key K) *Element[K, V]

GetElement returns the element for a key. If the key does not exist, the pointer will be nil.

func (*OrderedMap[K, V]) GetOrDefault

func (m *OrderedMap[K, V]) GetOrDefault(key K, defaultValue V) V

GetOrDefault returns the value for a key. If the key does not exist, returns the default value instead.

func (*OrderedMap[K, V]) Keys

func (m *OrderedMap[K, V]) Keys() (keys []K)

Keys returns all of the keys in the order they were inserted. If a key was replaced it will retain the same position. To ensure most recently set keys are always at the end you must always Delete before Set.

func (*OrderedMap[K, V]) Len

func (m *OrderedMap[K, V]) Len() int

Len returns the number of elements in the map.

func (*OrderedMap[K, V]) ReplaceKey

func (m *OrderedMap[K, V]) ReplaceKey(originalKey, newKey K) bool

ReplaceKey replaces an existing key with a new key while preserving order of the value. This function will return true if the operation was successful, or false if 'originalKey' is not found OR 'newKey' already exists (which would be an overwrite).

func (*OrderedMap[K, V]) Set

func (m *OrderedMap[K, V]) Set(key K, value V) bool

Set will set (or replace) a value for a key. If the key was new, then true will be returned. The returned value will be false if the value was replaced (even if the value was the same).

type SizeBuff

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

SizeBuff 有最大值限制的buff

func NewSizeBuff

func NewSizeBuff(size int) *SizeBuff

func (*SizeBuff) Bytes

func (s *SizeBuff) Bytes() []byte

func (*SizeBuff) Reader

func (s *SizeBuff) Reader() *bytes.Reader

func (*SizeBuff) Write

func (s *SizeBuff) Write(p []byte) (int, error)

type SortAbleSlice

type SortAbleSlice[T SortAbleType] []T

SortAbleSlice 可排序列表,从小到大

func (SortAbleSlice[T]) Len

func (x SortAbleSlice[T]) Len() int

func (SortAbleSlice[T]) Less

func (x SortAbleSlice[T]) Less(i, j int) bool

func (SortAbleSlice[T]) Swap

func (x SortAbleSlice[T]) Swap(i, j int)

type SortAbleType

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

type SyncMap

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

func NewSyncMap

func NewSyncMap[K comparable, V any]() *SyncMap[K, V]

func (*SyncMap[K, V]) Delete

func (m *SyncMap[K, V]) Delete(key K)

func (*SyncMap[K, V]) DeleteAll

func (m *SyncMap[K, V]) DeleteAll()

func (*SyncMap[K, V]) Len

func (m *SyncMap[K, V]) Len() int

func (*SyncMap[K, V]) Load

func (m *SyncMap[K, V]) Load(key K) (value V, ok bool)

func (*SyncMap[K, V]) LoadAndDelete

func (m *SyncMap[K, V]) LoadAndDelete(key K) (value V, loaded bool)

func (*SyncMap[K, V]) LoadOrStore

func (m *SyncMap[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool)

func (*SyncMap[K, V]) Range

func (m *SyncMap[K, V]) Range(f func(key K, value V) bool)

func (*SyncMap[K, V]) Store

func (m *SyncMap[K, V]) Store(key K, value V)

type UnsignedIntType

type UnsignedIntType interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64
}

Directories

Path Synopsis
Package flock implements a thread-safe interface for file locking.
Package flock implements a thread-safe interface for file locking.

Jump to

Keyboard shortcuts

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