Documentation
¶
Overview ¶
Package utils 提供通用工具函数。
核心功能:
- 字符串、切片、Map 操作
- 时间、数字处理
- 指针辅助、异步工具
- ID 生成(UUID/Snowflake)
使用示例:
id := utils.UUID()
ptr := utils.Ptr("value")
result := utils.Map(slice, func(v int) int { return v * 2 })
Index ¶
- Constants
- func Abs[T constraints.Signed | constraints.Float](n T) T
- func Age(birthday time.Time) int
- func All[T any](slice []T, predicate func(T) bool) bool
- func Any[T any](slice []T, predicate func(T) bool) bool
- func Avg[T constraints.Integer | constraints.Float](nums ...T) float64
- func BytesToString(b []byte) string
- func Chunk[T any](slice []T, size int) [][]T
- func Clamp[T constraints.Ordered](value, min, max T) T
- func Contains[T comparable](slice []T, item T) bool
- func DaysBetween(t1, t2 time.Time) int
- func Debounce(fn func(), delay time.Duration) func()
- func DefaultIfBlank(s, defaultVal string) string
- func DefaultIfEmpty(s, defaultVal string) string
- func EndOfDay(t time.Time) time.Time
- func EndOfMonth(t time.Time) time.Time
- func Filter[T any](slice []T, predicate func(T) bool) []T
- func FilterMap[K comparable, V any](m map[K]V, predicate func(K, V) bool) map[K]V
- func Find[T any](slice []T, predicate func(T) bool) (T, bool)
- func FindIndex[T any](slice []T, predicate func(T) bool) int
- func First[T any](slice []T) (T, bool)
- func Flatten[T any](slices [][]T) []T
- func FormatDate(t time.Time) string
- func FormatDateTime(t time.Time) string
- func FormatISO8601(t time.Time) string
- func FormatTime(t time.Time) string
- func FromUnixMilli(ms int64) time.Time
- func GetOrDefault[K comparable, V any](m map[K]V, key K, defaultVal V) V
- func GroupBy[T any, K comparable](slice []T, keyFunc func(T) K) map[K][]T
- func InRange[T constraints.Ordered](value, min, max T) bool
- func IndexOf[T comparable](slice []T, item T) int
- func Invert[K, V comparable](m map[K]V) map[V]K
- func IsBlank(s string) bool
- func IsEmpty(s string) bool
- func IsEven[T constraints.Integer](n T) bool
- func IsNegative[T constraints.Signed | constraints.Float](n T) bool
- func IsNil[T any](p *T) bool
- func IsNotBlank(s string) bool
- func IsNotEmpty(s string) bool
- func IsNotNil[T any](p *T) bool
- func IsOdd[T constraints.Integer](n T) bool
- func IsPositive[T constraints.Signed | constraints.Float](n T) bool
- func IsSameDay(t1, t2 time.Time) bool
- func IsToday(t time.Time) bool
- func IsWeekend(t time.Time) bool
- func IsYesterday(t time.Time) bool
- func IsZero[T constraints.Integer | constraints.Float](n T) bool
- func Keys[K comparable, V any](m map[K]V) []K
- func Last[T any](slice []T) (T, bool)
- func Map[T, R any](slice []T, mapper func(T) R) []R
- func MapKeys[K1, K2 comparable, V any](m map[K1]V, mapper func(K1) K2) map[K2]V
- func MapValues[K comparable, V1, V2 any](m map[K]V1, mapper func(V1) V2) map[K]V2
- func MaskEmail(email string) string
- func MaskIDCard(idCard string) string
- func MaskPhone(phone string) string
- func Max[T constraints.Ordered](a, b T) T
- func MaxSlice[T constraints.Ordered](nums []T) (T, bool)
- func Merge[K comparable, V any](maps ...map[K]V) map[K]V
- func Min[T constraints.Ordered](a, b T) T
- func MinSlice[T constraints.Ordered](nums []T) (T, bool)
- func NanoID() string
- func Omit[K comparable, V any](m map[K]V, keys ...K) map[K]V
- func OrderedID() string
- func Parallel(fns ...func() error) error
- func ParallelWithContext(ctx context.Context, fns ...func(context.Context) error) error
- func ParseDate(s string) (time.Time, error)
- func ParseDateTime(s string) (time.Time, error)
- func ParseISO8601(s string) (time.Time, error)
- func Pick[K comparable, V any](m map[K]V, keys ...K) map[K]V
- func Ptr[T any](v T) *T
- func RandomBase64(length int) string
- func RandomID(length int) string
- func Reduce[T, R any](slice []T, initial R, reducer func(R, T) R) R
- func Retry(ctx context.Context, fn func() error, cfg RetryConfig) error
- func RetryWithResult[T any](ctx context.Context, fn func() (T, error), cfg RetryConfig) (T, error)
- func Reverse(s string) string
- func ReverseSlice[T any](slice []T) []T
- func SetSnowflakeNode(nodeID int64)
- func ShortID(length int) string
- func Snowflake() int64
- func SnowflakeString() string
- func StartOfDay(t time.Time) time.Time
- func StartOfMonth(t time.Time) time.Time
- func StartOfWeek(t time.Time) time.Time
- func StartOfYear(t time.Time) time.Time
- func StringToBytes(s string) []byte
- func Sum[T constraints.Integer | constraints.Float](nums ...T) T
- func Throttle(fn func(), interval time.Duration) func()
- func ToCamelCase(s string) string
- func ToFloat64[T constraints.Integer | constraints.Float](n T) float64
- func ToInt[T constraints.Integer | constraints.Float](n T) int
- func ToInt64[T constraints.Integer | constraints.Float](n T) int64
- func ToPascalCase(s string) string
- func ToSnakeCase(s string) string
- func Truncate(s string, maxLen int) string
- func TruncateWithSuffix(s string, maxLen int, suffix string) string
- func UUID() string
- func UUIDShort() string
- func Unique[T comparable](slice []T) []T
- func UnixMilli(t time.Time) int64
- func Val[T any](p *T) T
- func ValOr[T any](p *T, defaultVal T) T
- func Values[K comparable, V any](m map[K]V) []V
- func WithTimeout[T any](ctx context.Context, timeout time.Duration, ...) (T, error)
- type Optional
- type RetryConfig
Constants ¶
View Source
const ( DateFormat = "2006-01-02" TimeFormat = "15:04:05" DateTimeFormat = "2006-01-02 15:04:05" ISO8601Format = "2006-01-02T15:04:05Z07:00" )
常用时间格式
Variables ¶
This section is empty.
Functions ¶
func FilterMap ¶
func FilterMap[K comparable, V any](m map[K]V, predicate func(K, V) bool) map[K]V
FilterMap 过滤 map
func GetOrDefault ¶
func GetOrDefault[K comparable, V any](m map[K]V, key K, defaultVal V) V
GetOrDefault 获取值,不存在返回默认值
func InRange ¶
func InRange[T constraints.Ordered](value, min, max T) bool
InRange 检查值是否在范围内 [min, max]
func IsNegative ¶
func IsNegative[T constraints.Signed | constraints.Float](n T) bool
IsNegative 是否为负数
func IsPositive ¶
func IsPositive[T constraints.Signed | constraints.Float](n T) bool
IsPositive 是否为正数
func MapKeys ¶
func MapKeys[K1, K2 comparable, V any](m map[K1]V, mapper func(K1) K2) map[K2]V
MapKeys 转换 map 的键
func MapValues ¶
func MapValues[K comparable, V1, V2 any](m map[K]V1, mapper func(V1) V2) map[K]V2
MapValues 转换 map 的值
func ParallelWithContext ¶
ParallelWithContext 带 context 的并行执行
func RetryWithResult ¶
RetryWithResult 重试执行带返回值的函数
func ToFloat64 ¶
func ToFloat64[T constraints.Integer | constraints.Float](n T) float64
ToFloat64 安全转换为 float64
func ToInt64 ¶
func ToInt64[T constraints.Integer | constraints.Float](n T) int64
ToInt64 安全转换为 int64
func ToPascalCase ¶
ToPascalCase 转换为帕斯卡命名 (snake_case -> SnakeCase)
func TruncateWithSuffix ¶
TruncateWithSuffix 截断字符串并添加后缀
Types ¶
type Optional ¶
type Optional[T any] struct { // contains filtered or unexported fields }
Optional 可选值
func FlatMapOpt ¶
FlatMapOpt 扁平化转换
Click to show internal directories.
Click to hide internal directories.