utils

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2025 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Default = NewUtils()

全局工具实例

Functions

func CamelToSnake

func CamelToSnake(str string) string

CamelToSnake 驼峰命名转蛇形命名

func ClampFloat

func ClampFloat(value, min, max float64) float64

ClampFloat 将浮点数限制在指定范围内

func ClampInt

func ClampInt(value, min, max int) int

ClampInt 将整数限制在指定范围内

func Coalesce

func Coalesce(values ...interface{}) interface{}

Coalesce 返回第一个非空值

func CoalesceString

func CoalesceString(values ...string) string

CoalesceString 返回第一个非空字符串

func CompactJSON

func CompactJSON(data interface{}) (string, error)

CompactJSON 压缩JSON输出

func Contains

func Contains(slice []interface{}, item interface{}) bool

Contains 检查切片是否包含指定元素

func ContainsInt

func ContainsInt(slice []int, item int) bool

ContainsInt 检查整数切片是否包含指定整数

func ContainsString

func ContainsString(slice []string, item string) bool

ContainsString 检查字符串切片是否包含指定字符串

func CopyStruct

func CopyStruct(src, dst interface{}) error

CopyStruct 复制结构体(浅拷贝)

func EnsureDir

func EnsureDir(path string) error

EnsureDir 确保目录存在,不存在则创建

func FormatDuration

func FormatDuration(duration time.Duration) string

FormatDuration 格式化时间间隔为人类可读格式

func GenerateSecureToken

func GenerateSecureToken(length int) string

GenerateSecureToken 生成安全令牌

func GetFileExtension

func GetFileExtension(filename string) string

GetFileExtension 获取文件扩展名

func GetFileSize

func GetFileSize(path string) (int64, error)

GetFileSize 获取文件大小

func GetMapKeys

func GetMapKeys(m map[string]interface{}) []string

GetMapKeys 获取map的所有键

func GetMapValues

func GetMapValues(m map[string]interface{}) []interface{}

GetMapValues 获取map的所有值

func GetStructFields

func GetStructFields(obj interface{}) []string

GetStructFields 获取结构体字段信息

func If

func If(condition bool, trueVal, falseVal interface{}) interface{}

If 三元运算符

func IfInt

func IfInt(condition bool, trueVal, falseVal int) int

IfInt 整数三元运算符

func IfString

func IfString(condition bool, trueVal, falseVal string) string

IfString 字符串三元运算符

func IgnoreError

func IgnoreError(value interface{}, err error) interface{}

IgnoreError 忽略错误,只返回值

func InRange

func InRange(value, min, max int) bool

InRange 检查数字是否在指定范围内

func IsBusinessDay

func IsBusinessDay(t time.Time) bool

IsBusinessDay 检查是否为工作日

func IsEmpty

func IsEmpty(str string) bool

IsEmpty 检查字符串是否为空或只包含空白字符

func IsNil

func IsNil(i interface{}) bool

IsNil 检查接口是否为nil

func IsNotEmpty

func IsNotEmpty(str string) bool

IsNotEmpty 检查字符串是否不为空

func IsStrongPassword

func IsStrongPassword(password string) bool

IsStrongPassword 验证强密码

func IsValidEmail

func IsValidEmail(email string) bool

IsValidEmail 验证电子邮件地址

func IsValidJSON

func IsValidJSON(jsonStr string) bool

IsValidJSON 验证JSON格式

func IsValidPath

func IsValidPath(path string) bool

IsValidPath 检查路径是否有效

func IsValidPhone

func IsValidPhone(phone string) bool

IsValidPhone 验证电话号码(简单版本)

func IsValidURL

func IsValidURL(urlStr string) bool

IsValidURL 验证URL

func IsZero

func IsZero(i interface{}) bool

IsZero 检查值是否为零值

func JSONToMap

func JSONToMap(jsonStr string) (map[string]interface{}, error)

JSONToMap 将JSON字符串转换为map

func MD5Hash

func MD5Hash(data string) string

MD5Hash 计算MD5哈希

func MaskString

func MaskString(str string, start, end int, mask rune) string

MaskString 掩码字符串敏感信息

func MergeMaps

func MergeMaps(maps ...map[string]interface{}) map[string]interface{}

MergeMaps 合并多个map

func Must

func Must(err error)

Must 如果有错误则panic

func MustReturn

func MustReturn(value interface{}, err error) interface{}

MustReturn 如果有错误则panic,否则返回值

func NextBusinessDay

func NextBusinessDay(t time.Time) time.Time

NextBusinessDay 获取下一个工作日

func ParseTimeWithFormats

func ParseTimeWithFormats(timeStr string, formats []string) (time.Time, error)

ParseTimeWithFormats 尝试使用多种格式解析时间

func PrettyJSON

func PrettyJSON(data interface{}) (string, error)

PrettyJSON 美化JSON输出

func RandomString

func RandomString(length int) string

RandomString 生成随机字符串

func Retry

func Retry(attempts int, delay time.Duration, fn func() error) error

Retry 重试执行函数

func RetryWithBackoff

func RetryWithBackoff(attempts int, initialDelay time.Duration, backoffFactor float64, fn func() error) error

RetryWithBackoff 带退避的重试

func SHA256Hash

func SHA256Hash(data string) string

SHA256Hash 计算SHA256哈希

func SafeParseFloat

func SafeParseFloat(str string, defaultValue float64) float64

SafeParseFloat 安全解析浮点数,失败时返回默认值

func SafeParseInt

func SafeParseInt(str string, defaultValue int) int

SafeParseInt 安全解析整数,失败时返回默认值

func SnakeToCamel

func SnakeToCamel(str string) string

SnakeToCamel 蛇形命名转驼峰命名

func TruncateString

func TruncateString(str string, length int) string

TruncateString 截断字符串到指定长度

func UniqueInts

func UniqueInts(slice []int) []int

UniqueInts 去重整数切片

func UniqueStrings

func UniqueStrings(slice []string) []string

UniqueStrings 去重字符串切片

Types

type Converter

type Converter interface {
	Convert(value interface{}) (interface{}, error)
}

Converter 转换器接口

type CryptoUtils

type CryptoUtils struct{}

func Crypto

func Crypto() *CryptoUtils

func (*CryptoUtils) GenerateToken

func (c *CryptoUtils) GenerateToken(length int) string

func (*CryptoUtils) MD5

func (c *CryptoUtils) MD5(data string) string

CryptoUtils 方法

func (*CryptoUtils) SHA256

func (c *CryptoUtils) SHA256(data string) string

type Encoder

type Encoder interface {
	Encode(data interface{}) (string, error)
	Decode(data string, v interface{}) error
}

Encoder 编码器接口

type FileUtils

type FileUtils struct{}

func File

func File() *FileUtils

func (*FileUtils) EnsureDir

func (f *FileUtils) EnsureDir(path string) error

FileUtils 方法

func (*FileUtils) GetExtension

func (f *FileUtils) GetExtension(filename string) string

func (*FileUtils) GetSize

func (f *FileUtils) GetSize(path string) (int64, error)

func (*FileUtils) IsValidPath

func (f *FileUtils) IsValidPath(path string) bool

type JSONUtils

type JSONUtils struct{}

func JSON

func JSON() *JSONUtils

func (*JSONUtils) Compact

func (j *JSONUtils) Compact(data interface{}) (string, error)

func (*JSONUtils) IsValid

func (j *JSONUtils) IsValid(jsonStr string) bool

func (*JSONUtils) Pretty

func (j *JSONUtils) Pretty(data interface{}) (string, error)

JSONUtils 方法

func (*JSONUtils) ToMap

func (j *JSONUtils) ToMap(jsonStr string) (map[string]interface{}, error)

type NumberUtils

type NumberUtils struct{}

func Number

func Number() *NumberUtils

func (*NumberUtils) ClampFloat

func (n *NumberUtils) ClampFloat(value, min, max float64) float64

func (*NumberUtils) ClampInt

func (n *NumberUtils) ClampInt(value, min, max int) int

func (*NumberUtils) InRange

func (n *NumberUtils) InRange(value, min, max int) bool

func (*NumberUtils) SafeParseFloat

func (n *NumberUtils) SafeParseFloat(str string, defaultValue float64) float64

func (*NumberUtils) SafeParseInt

func (n *NumberUtils) SafeParseInt(str string, defaultValue int) int

NumberUtils 方法

type ReflectUtils

type ReflectUtils struct{}

func Reflect

func Reflect() *ReflectUtils

func (*ReflectUtils) Copy

func (r *ReflectUtils) Copy(src, dst interface{}) error

func (*ReflectUtils) GetFields

func (r *ReflectUtils) GetFields(obj interface{}) []string

func (*ReflectUtils) IsNil

func (r *ReflectUtils) IsNil(i interface{}) bool

ReflectUtils 方法

func (*ReflectUtils) IsZero

func (r *ReflectUtils) IsZero(i interface{}) bool

type StringUtils

type StringUtils struct{}

具体工具类型定义

func String

func String() *StringUtils

全局便捷函数

func (*StringUtils) CamelToSnake

func (s *StringUtils) CamelToSnake(str string) string

func (*StringUtils) IsEmpty

func (s *StringUtils) IsEmpty(str string) bool

StringUtils 方法

func (*StringUtils) IsNotEmpty

func (s *StringUtils) IsNotEmpty(str string) bool

func (*StringUtils) Mask

func (s *StringUtils) Mask(str string, start, end int, mask rune) string

func (*StringUtils) Random

func (s *StringUtils) Random(length int) string

func (*StringUtils) SnakeToCamel

func (s *StringUtils) SnakeToCamel(str string) string

func (*StringUtils) Truncate

func (s *StringUtils) Truncate(str string, length int) string

type TimeUtils

type TimeUtils struct{}

func Time

func Time() *TimeUtils

func (*TimeUtils) FormatDuration

func (t *TimeUtils) FormatDuration(duration time.Duration) string

TimeUtils 方法

func (*TimeUtils) IsBusinessDay

func (t *TimeUtils) IsBusinessDay(time time.Time) bool

func (*TimeUtils) NextBusinessDay

func (t *TimeUtils) NextBusinessDay(time time.Time) time.Time

func (*TimeUtils) ParseWithFormats

func (t *TimeUtils) ParseWithFormats(timeStr string, formats []string) (time.Time, error)

type Utils

type Utils struct{}

Utils 工具集合

func NewUtils

func NewUtils() *Utils

NewUtils 创建工具实例

func (*Utils) Crypto

func (u *Utils) Crypto() *CryptoUtils

Crypto 加密工具方法

func (*Utils) File

func (u *Utils) File() *FileUtils

File 文件工具方法

func (*Utils) JSON

func (u *Utils) JSON() *JSONUtils

JSON JSON工具方法

func (*Utils) Number

func (u *Utils) Number() *NumberUtils

Number 数字工具方法

func (*Utils) Reflect

func (u *Utils) Reflect() *ReflectUtils

Reflect 反射工具方法

func (*Utils) String

func (u *Utils) String() *StringUtils

String 字符串工具方法

func (*Utils) Time

func (u *Utils) Time() *TimeUtils

Time 时间工具方法

func (*Utils) Validation

func (u *Utils) Validation() *ValidationUtils

Validation 验证工具方法

type ValidationUtils

type ValidationUtils struct{}

func Validation

func Validation() *ValidationUtils

func (*ValidationUtils) IsEmail

func (v *ValidationUtils) IsEmail(email string) bool

ValidationUtils 方法

func (*ValidationUtils) IsPhone

func (v *ValidationUtils) IsPhone(phone string) bool

func (*ValidationUtils) IsStrongPassword

func (v *ValidationUtils) IsStrongPassword(password string) bool

func (*ValidationUtils) IsURL

func (v *ValidationUtils) IsURL(url string) bool

type Validator

type Validator interface {
	Validate(value interface{}) error
}

Validator 验证器接口

Jump to

Keyboard shortcuts

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