竹简文档

概述

xUtil 提供字符串、时间、加密、验证等常用工具函数

工具函数

xUtil 提供丰富的工具函数,涵盖字符串处理、时间操作、数据验证、加密哈希、密码处理等常用场景。

import xUtil "github.com/xiaolfeng/bamboo-base-go/common/utility"

功能分类

字段

类型

调用方式

所有工具函数采用链式调用方式:

// 字符串工具
xUtil.Str().IsBlank("  ")                    // true
xUtil.Str().DefaultIfBlank("", "default")   // "default"

// 时间工具
xUtil.Timer().FormatNow("2006-01-02")        // "2026-02-02"
xUtil.Timer().StartOfDay(time.Now())        // 今天 00:00:00

// 验证工具
xUtil.Valid().IsPhone("13812345678")         // true
xUtil.Valid().IsEmail("test@example.com")    // true

// 密码工具
hash, _ := xUtil.Password().Encrypt("myPassword123")
xUtil.Password().IsValid("myPassword123", hash)  // true

// 生成工具
code := xUtil.Generate().RandomString(16)          // "aB3xY9kLmN2pQ5wR"
numCode := xUtil.Generate().RandomNumberString(6)  // "384729"

// 安全密钥工具
key := xUtil.Security().GenerateKey()               // "cs_a1b2c3d4..."

快速示例

字符串处理

// 空白检查
xUtil.Str().IsBlank("  ")                    // true
xUtil.Str().DefaultIfBlank("", "default")    // "default"

// 字符串脱敏
xUtil.Str().Mask("13812345678", 3, 4, "*")  // "138****5678"

// 命名转换
xUtil.Str().CamelToSnake("userName")         // "user_name"

时间处理

// 格式化
dateStr := xUtil.Timer().FormatNow("2006-01-02")  // "2026-02-02"

// 时间范围
start := xUtil.Timer().StartOfDay(time.Now())   // 今天 00:00:00
end := xUtil.Timer().EndOfMonth(time.Now())     // 本月最后一天 23:59:59

// 年龄计算
age := xUtil.Timer().Age(birthday)              // 26

数据验证

// 格式验证
xUtil.Valid().IsPhone("13812345678")         // true
xUtil.Valid().IsEmail("test@example.com")    // true
xUtil.Valid().IsUUID("550e8400-...")         // true

// 密码强度
xUtil.Valid().IsStrongPassword("Abc123!@#")   // true

密码加密

// 加密密码
hash, _ := xUtil.Password().Encrypt("myPassword123")

// 验证密码
isValid := xUtil.Password().IsValid("myPassword123", hash)  // true

随机生成

// 随机字符串
code := xUtil.Generate().RandomString(16)          // "aB3xY9kLmN2pQ5wR"
numCode := xUtil.Generate().RandomNumberString(6)  // "384729"

// 安全密钥
key := xUtil.Security().GenerateKey()               // "cs_a1b2c3d4..."

下一步

On this page