utils

package
v0.0.0-...-118bb3a Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2024 License: MIT Imports: 24 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AESDecrypt

func AESDecrypt(ciphertext []byte, key []byte) ([]byte, error)

AESDecrypt AES解密

func AESEncrypt

func AESEncrypt(plaintext []byte, key []byte) ([]byte, error)

AESEncrypt AES加密

func AddDays

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

AddDays 添加天数

func AddMonths

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

AddMonths 添加月数

func AddYears

func AddYears(t time.Time, years int) time.Time

AddYears 添加年数

func AppendToFile

func AppendToFile(path string, content string) error

AppendToFile 追加内容到文件

func Base64Decode

func Base64Decode(text string) ([]byte, error)

Base64Decode Base64解码

func Base64Encode

func Base64Encode(data []byte) string

Base64Encode Base64编码

func CamelCase

func CamelCase(str string) string

CamelCase 将字符串转换为驼峰命名

func Capitalize

func Capitalize(str string) string

Capitalize 首字母大写

func Ceil

func Ceil(x float64, precision int) float64

Ceil 向上取整到指定小数位

func Chunk

func Chunk[T any](arr []T, size int) [][]T

Chunk 将数组分割成指定大小的块

func Clamp

func Clamp[T ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~float32 | ~float64](value, min, max T) T

Clamp 将值限制在指定范围内

func CleanDirectory

func CleanDirectory(path string) error

CleanDirectory 清空目录

func Clear

func Clear[K comparable, V any](m map[K]V)

Clear 清空map

func Clone

func Clone[K comparable, V any](m map[K]V) map[K]V

Clone 克隆map

func CompareHashes

func CompareHashes(hash1, hash2 string) bool

CompareHashes 安全比较两个哈希值

func CompareVersion

func CompareVersion(v1, v2 string) int

CompareVersion 比较两个版本号

func Contains

func Contains[T comparable](arr []T, target T) bool

Contains 检查数组是否包含某个元素

func CopyDirectory

func CopyDirectory(src, dst string) error

CopyDirectory 复制目录

func CopyFile

func CopyFile(src, dst string) error

CopyFile 复制文件

func Count

func Count[T any](arr []T, predicate func(T) bool) int

Count 统计满足条件的元素个数

func CountMap

func CountMap[K comparable, V any](m map[K]V, predicate func(K, V) bool) int

Count 统计满足条件的键值对个数

func CountWords

func CountWords(str string) int

CountWords 统计单词数量

func CreateDirIfNotExists

func CreateDirIfNotExists(path string) error

CreateDirIfNotExists 如果目录不存在则创建

func CreateDirectory

func CreateDirectory(path string) error

CreateDirectory 创建目录

func DecryptString

func DecryptString(ciphertext string, key []byte) (string, error)

DecryptString 解密字符串

func DeleteFile

func DeleteFile(path string) error

DeleteFile 删除文件

func Difference

func Difference[T comparable](arr1 []T, arr2 []T) []T

Difference 获取数组的差集

func DirExists

func DirExists(path string) bool

DirExists 检查目录是否存在

func Distance

func Distance(x1, y1, x2, y2 float64) float64

Distance 计算两点之间的距离

func EncryptString

func EncryptString(plaintext string, key []byte) (string, error)

EncryptString 加密字符串

func Entries

func Entries[K comparable, V any](m map[K]V) [][2]interface{}

Entries 获取map的所有键值对

func Every

func Every[T any](arr []T, predicate func(T) bool) bool

Every 检查是否所有元素都满足条件

func EveryMap

func EveryMap[K comparable, V any](m map[K]V, predicate func(K, V) bool) bool

Every 检查是否所有键值对都满足条件

func ExtractEmails

func ExtractEmails(text string) []string

ExtractEmails 从文本中提取所有邮箱地址

func ExtractURLs

func ExtractURLs(text string) []string

ExtractURLs 从文本中提取所有URL

func Factorial

func Factorial(n int) int

Factorial 计算阶乘

func FileExists

func FileExists(path string) bool

FileExists 检查文件是否存在

func Filter

func Filter[T any](arr []T, predicate func(T) bool) []T

Filter 过滤数组

func FilterMap

func FilterMap[K comparable, V any](m map[K]V, predicate func(K, V) bool) map[K]V

Filter 过滤map

func Find

func Find[T any](arr []T, predicate func(T) bool) (T, bool)

Find 查找满足条件的第一个元素

func FindInMap

func FindInMap[K comparable, V any](m map[K]V, predicate func(K, V) bool) (K, V, bool)

Find 查找满足条件的第一个键值对

func FindIndex

func FindIndex[T any](arr []T, predicate func(T) bool) int

FindIndex 查找满足条件的第一个元素的索引

func Flatten

func Flatten[T any](arr [][]T) []T

Flatten 展平嵌套数组

func Floor

func Floor(x float64, precision int) float64

Floor 向下取整到指定小数位

func ForEach

func ForEach[K comparable, V any](m map[K]V, fn func(K, V))

ForEach 遍历map

func FormatCurrency

func FormatCurrency(amount float64, currency string) string

FormatCurrency 格式化货币

func FormatDuration

func FormatDuration(d time.Duration) string

FormatDuration 格式化时间间隔

func FormatFileSize

func FormatFileSize(size int64) string

FormatFileSize 格式化文件大小

func FormatPercent

func FormatPercent(value float64) string

FormatPercent 格式化百分比

func FormatTemplate

func FormatTemplate(template string, data map[string]interface{}) string

FormatTemplate 简单的模板格式化

func FormatThousands

func FormatThousands(n int64) string

FormatThousands 格式化数字为千分位

func FormatTime

func FormatTime(t time.Time, layout string) string

FormatTime 格式化时间

func FromEntries

func FromEntries[K comparable, V any](entries [][2]interface{}) map[K]V

FromEntries 从键值对数组创建map

func GCD

func GCD(a, b int) int

GCD 计算最大公约数

func GenerateAESKey

func GenerateAESKey(bits int) ([]byte, error)

GenerateAESKey 生成AES密钥

func GenerateRandomBytes

func GenerateRandomBytes(length int) ([]byte, error)

GenerateRandomBytes 生成指定长度的随机字节数组

func GenerateRandomString

func GenerateRandomString(length int) (string, error)

GenerateRandomString 生成指定长度的随机字符串

func GetAge

func GetAge(birthDate time.Time) int

GetAge 计算年龄

func GetCurrentMicroseconds

func GetCurrentMicroseconds() int64

GetCurrentMicroseconds 获取当前微秒时间戳

func GetCurrentMilliseconds

func GetCurrentMilliseconds() int64

GetCurrentMilliseconds 获取当前毫秒时间戳

func GetCurrentNanoseconds

func GetCurrentNanoseconds() int64

GetCurrentNanoseconds 获取当前纳秒时间戳

func GetCurrentTime

func GetCurrentTime() time.Time

GetCurrentTime 获取当前时间

func GetCurrentTimestamp

func GetCurrentTimestamp() int64

GetCurrentTimestamp 获取当前时间戳

func GetDaysInMonth

func GetDaysInMonth(year int, month time.Month) int

GetDaysInMonth 获取某月的天数

func GetDirectoryOwner

func GetDirectoryOwner(path string) (uint32, uint32, error)

GetDirectoryOwner 获取目录所有者

func GetDirectoryPermissions

func GetDirectoryPermissions(path string) (os.FileMode, error)

GetDirectoryPermissions 获取目录权限

func GetDirectorySize

func GetDirectorySize(path string) (int64, error)

GetDirectorySize 获取目录大小

func GetEndOfDay

func GetEndOfDay(t time.Time) time.Time

GetEndOfDay 获取一天的结束时间

func GetEndOfMonth

func GetEndOfMonth(t time.Time) time.Time

GetEndOfMonth 获取一个月的结束时间

func GetEndOfQuarter

func GetEndOfQuarter(t time.Time) time.Time

GetEndOfQuarter 获取季度的结束时间

func GetEndOfWeek

func GetEndOfWeek(t time.Time) time.Time

GetEndOfWeek 获取一周的结束时间(周日)

func GetEndOfYear

func GetEndOfYear(t time.Time) time.Time

GetEndOfYear 获取一年的结束时间

func GetFileExtension

func GetFileExtension(path string) string

GetFileExtension 获取文件扩展名

func GetFileMD5

func GetFileMD5(path string) (string, error)

GetFileMD5 计算文件的MD5值

func GetFileModTime

func GetFileModTime(path string) (int64, error)

GetFileModTime 获取文件修改时间

func GetFileName

func GetFileName(path string) string

GetFileName 获取文件名(不含扩展名)

func GetFileOwner

func GetFileOwner(path string) (uint32, uint32, error)

GetFileOwner 获取文件所有者

func GetFilePermissions

func GetFilePermissions(path string) (os.FileMode, error)

GetFilePermissions 获取文件权限

func GetFileSize

func GetFileSize(path string) (int64, error)

GetFileSize 获取文件大小

func GetFiles

func GetFiles(path string) ([]string, error)

GetFiles 获取所有文件

func GetFilesWithExtension

func GetFilesWithExtension(path string, ext string) ([]string, error)

GetFilesWithExtension 获取指定扩展名的文件

func GetOrCompute

func GetOrCompute[K comparable, V any](m map[K]V, key K, computer func() V) V

GetOrCompute 获取map中的值,如果不存在则计算、设置并返回新值

func GetOrDefault

func GetOrDefault[K comparable, V any](m map[K]V, key K, defaultValue V) V

GetOrDefault 获取map中的值,如果不存在则返回默认值

func GetOrSet

func GetOrSet[K comparable, V any](m map[K]V, key K, defaultValue V) V

GetOrSet 获取map中的值,如果不存在则设置并返回默认值

func GetQuarter

func GetQuarter(t time.Time) int

GetQuarter 获取季度

func GetStartOfDay

func GetStartOfDay(t time.Time) time.Time

GetStartOfDay 获取一天的开始时间

func GetStartOfMonth

func GetStartOfMonth(t time.Time) time.Time

GetStartOfMonth 获取一个月的开始时间

func GetStartOfQuarter

func GetStartOfQuarter(t time.Time) time.Time

GetStartOfQuarter 获取季度的开始时间

func GetStartOfWeek

func GetStartOfWeek(t time.Time) time.Time

GetStartOfWeek 获取一周的开始时间(周一)

func GetStartOfYear

func GetStartOfYear(t time.Time) time.Time

GetStartOfYear 获取一年的开始时间

func GetSubdirectories

func GetSubdirectories(path string) ([]string, error)

GetSubdirectories 获取所有子目录

func GetWeekday

func GetWeekday(t time.Time) string

GetWeekday 获取星期几

func GroupBy

func GroupBy[T any, K comparable](arr []T, keySelector func(T) K) map[K][]T

GroupBy 按条件对数组元素进行分组

func GroupByMap

func GroupByMap[K comparable, V any, G comparable](m map[K]V, keySelector func(K, V) G) map[G]map[K]V

GroupBy 按条件对map进行分组

func HasKey

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

HasKey 检查map是否包含指定的键

func HasValue

func HasValue[K comparable, V comparable](m map[K]V, value V) bool

HasValue 检查map是否包含指定的值

func HashPassword

func HashPassword(password string) string

HashPassword 对密码进行哈希处理

func IndexOf

func IndexOf[T comparable](arr []T, target T) int

IndexOf 获取元素在数组中的索引

func Intersection

func Intersection[T comparable](arrs ...[]T) []T

Intersection 获取多个数组的交集

func Invert

func Invert[K comparable, V comparable](m map[K]V) map[V]K

Invert 反转map的键值

func IsBinaryFile

func IsBinaryFile(path string) bool

IsBinaryFile 判断是否为二进制文件

func IsEmail

func IsEmail(email string) bool

IsEmail 验证邮箱地址格式

func IsEmptyDirectory

func IsEmptyDirectory(path string) (bool, error)

IsEmptyDirectory 检查目录是否为空

func IsLeapYear

func IsLeapYear(year int) bool

IsLeapYear 判断是否为闰年

func IsNumeric

func IsNumeric(s string) bool

IsNumeric 判断字符串是否为数字

func IsPrime

func IsPrime(n int) bool

IsPrime 判断是否为素数

func IsSameDay

func IsSameDay(t1, t2 time.Time) bool

IsSameDay 判断是否为同一天

func IsSameMonth

func IsSameMonth(t1, t2 time.Time) bool

IsSameMonth 判断是否为同一月

func IsSameQuarter

func IsSameQuarter(t1, t2 time.Time) bool

IsSameQuarter 判断是否为同一季度

func IsSameWeek

func IsSameWeek(t1, t2 time.Time) bool

IsSameWeek 判断是否为同一周

func IsSameYear

func IsSameYear(t1, t2 time.Time) bool

IsSameYear 判断是否为同一年

func IsTextFile

func IsTextFile(path string) bool

IsTextFile 判断是否为文本文件

func IsURL

func IsURL(url string) bool

IsURL 验证URL格式

func KebabCase

func KebabCase(str string) string

KebabCase 将字符串转换为短横线命名

func Keys

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

Keys 获取map的所有键

func LCM

func LCM(a, b int) int

LCM 计算最小公倍数

func LastIndexOf

func LastIndexOf[T comparable](arr []T, target T) int

LastIndexOf 获取元素在数组中最后出现的索引

func Lerp

func Lerp(start, end, t float64) float64

Lerp 线性插值

func ListDirectory

func ListDirectory(path string) ([]string, error)

ListDirectory 列出目录内容

func ListDirectoryWithFilter

func ListDirectoryWithFilter(path string, filter func(string) bool) ([]string, error)

ListDirectoryWithFilter 列出目录内容(带过滤器)

func MD5

func MD5(str string) string

MD5 计算字符串的MD5值

func MD5Hash

func MD5Hash(text string) string

MD5Hash 计算字符串的MD5哈希值

func Map

func Map[T any, U any](arr []T, mapper func(T) U) []U

Map 映射数组

func MapMap

func MapMap[K comparable, V any, U any](m map[K]V, mapper func(K, V) U) map[K]U

Map 映射map

func Max

func Max[T ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~float32 | ~float64](x, y T) T

Max 返回最大值

func Merge

func Merge[K comparable, V any](maps ...map[K]V) map[K]V

Merge 合并多个map

func Min

func Min[T ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~float32 | ~float64](x, y T) T

Min 返回最小值

func MoveDirectory

func MoveDirectory(src, dst string) error

MoveDirectory 移动目录

func MoveFile

func MoveFile(src, dst string) error

MoveFile 移动文件

func Omit

func Omit[K comparable, V any](m map[K]V, keys []K) map[K]V

Omit 从map中排除指定的键

func ParseFloat

func ParseFloat(s string, defaultVal float64) float64

ParseFloat 安全的将字符串转换为浮点数

func ParseInt

func ParseInt(s string, defaultVal int) int

ParseInt 安全的将字符串转换为整数

func ParseTime

func ParseTime(timeStr, layout string) (time.Time, error)

ParseTime 解析时间字符串

func ParseVersion

func ParseVersion(version string) (major, minor, patch int, err error)

ParseVersion 解析版本号

func Pick

func Pick[K comparable, V any](m map[K]V, keys []K) map[K]V

Pick 从map中选择指定的键

func RandomFloat

func RandomFloat(min, max float64) float64

RandomFloat 生成指定范围内的随机浮点数

func RandomInt

func RandomInt(min, max int) int

RandomInt 生成指定范围内的随机整数

func RandomString

func RandomString(length int) string

RandomString 生成指定长度的随机字符串

func ReadFile

func ReadFile(path string) (string, error)

ReadFile 读取文件内容

func ReadLines

func ReadLines(path string) ([]string, error)

ReadLines 读取文件的所有行

func Reduce

func Reduce[T any, U any](arr []T, reducer func(U, T) U, initial U) U

Reduce 归约数组

func Remove

func Remove[T comparable](arr []T, target T) []T

Remove 从数组中移除指定元素

func RemoveAll

func RemoveAll[T comparable](arr []T, target T) []T

RemoveAll 从数组中移除所有指定元素

func RemoveAt

func RemoveAt[T any](arr []T, index int) []T

RemoveAt 从数组中移除指定索引的元素

func RemoveDirectory

func RemoveDirectory(path string) error

RemoveDirectory 删除目录

func RemoveSpaces

func RemoveSpaces(str string) string

RemoveSpaces 移除字符串中的所有空白字符

func Reverse

func Reverse[T any](arr []T) []T

Reverse 反转数组

func ReverseString

func ReverseString(str string) string

ReverseString 反转字符串

func Round

func Round(x float64, precision int) float64

Round 四舍五入到指定小数位

func SHA1Hash

func SHA1Hash(text string) string

SHA1Hash 计算字符串的SHA1哈希值

func SHA256Hash

func SHA256Hash(text string) string

SHA256Hash 计算字符串的SHA256哈希值

func SHA512Hash

func SHA512Hash(text string) string

SHA512Hash 计算字符串的SHA512哈希值

func SetDirectoryOwner

func SetDirectoryOwner(path string, uid, gid int) error

SetDirectoryOwner 设置目录所有者

func SetDirectoryPermissions

func SetDirectoryPermissions(path string, mode os.FileMode) error

SetDirectoryPermissions 设置目录权限

func SetFileOwner

func SetFileOwner(path string, uid, gid int) error

SetFileOwner 设置文件所有者

func SetFilePermissions

func SetFilePermissions(path string, mode os.FileMode) error

SetFilePermissions 设置文件权限

func Shuffle

func Shuffle[T any](arr []T) []T

Shuffle 打乱数组

func Slugify

func Slugify(str string) string

Slugify 将字符串转换为URL友好的格式

func SnakeCase

func SnakeCase(str string) string

SnakeCase 将字符串转换为蛇形命名

func Some

func Some[T any](arr []T, predicate func(T) bool) bool

Some 检查是否存在元素满足条件

func SomeMap

func SomeMap[K comparable, V any](m map[K]V, predicate func(K, V) bool) bool

Some 检查是否存在键值对满足条件

func Sort

func Sort[T any](arr []T, less func(i, j T) bool) []T

Sort 排序数组

func ToFixed

func ToFixed(num float64, precision int) string

ToFixed 将浮点数格式化为指定小数位数的字符串

func TruncateString

func TruncateString(str string, length int) string

TruncateString 截断字符串到指定长度,并添加省略号

func Union

func Union[T comparable](arrs ...[]T) []T

Union 获取多个数组的并集

func Unique

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

Unique 获取数组的唯一值

func Update

func Update[K comparable, V any](m map[K]V, key K, updater func(V) V) bool

Update 更新map中的值

func Values

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

Values 获取map的所有值

func VerifyPassword

func VerifyPassword(password, hashedPassword string) bool

VerifyPassword 验证密码

func WrapText

func WrapText(text string, width int) string

WrapText 按指定宽度换行文本

func WriteFile

func WriteFile(path string, content string) error

WriteFile 写入文件内容

func WriteLines

func WriteLines(path string, lines []string) error

WriteLines 将多行内容写入文件

func Zip

func Zip[T any](arrs ...[]T) [][]T

Zip 将多个数组按索引组合

Types

This section is empty.

Jump to

Keyboard shortcuts

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