竹简文档

概述

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

工具函数

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

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

功能分类

字段

类型

快速示例

字符串处理

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

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

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

时间处理

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

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

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

数据验证

// 格式验证
xUtil.IsValidPhone("13812345678")       // true
xUtil.IsValidEmail("test@example.com")  // true
xUtil.IsValidUUID("550e8400-...")       // true

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

密码加密

// 加密密码
hash := xUtil.MustEncryptPasswordString("myPassword123")

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

随机生成

// 随机字符串
code := xUtil.GenerateRandomString(16)        // "aB3xY9kLmN2pQ5wR"
numCode := xUtil.GenerateRandomNumberString(6) // "384729"

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

下一步

On this page