utils

package
v1.1.7 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2026 License: MIT Imports: 39 Imported by: 3

Documentation

Index

Constants

View Source
const (
	TimeFmt  = "2006-01-02 15:04:05.000"
	TimeFmt2 = "2006-01-02 15:04:05.000000"
	DateFmt  = "2006-01-02"
	OneDay   = 86400000
	OneWeek  = OneDay * 7
	TwoWeek  = OneDay * 14
	OneMonth = OneDay * 30
)
View Source
const (
	MOBILE   = "^1[3456789]\\d{9}$"                                                                   // 手机号码
	INTEGER  = "^[\\-]?[1-9]+[0-9]*$|^[0]$"                                                           // 包含+-的自然数
	FLOAT    = "^[\\-]?[1-9]+[\\.][0-9]+$|^[\\-]?[0][\\.][0-9]+$"                                     // 包含+-的浮点数
	IPV4     = "^((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)$"           // IPV4地址
	EMAIL    = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$"                                    // 邮箱地址
	ACCOUNT  = "^[a-zA-Z][a-zA-Z0-9_]{5,14}$"                                                         // 账号格式
	PASSWORD = "^.{6,18}$"                                                                            // 密码格式
	URL      = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?$"                                   // URL格式
	IDNO     = "(^\\d{18}$)|(^\\d{15}$)"                                                              // 身份证格式
	PKNO     = "^1([3-9]{1})([0-9]{17})$"                                                             // 主键ID格式
	NUMBER   = "(^[1-9]([0-9]{0,29})$)|(^(0){1}$)"                                                    // 自然数
	NUMBER2  = "^[0-9]+$"                                                                             // 纯数字
	MONEY    = "(^[1-9]([0-9]{0,10})$)"                                                               // 自然数金额格式
	MONEY2   = "(^[1-9]([0-9]{0,12})?(\\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\\.[0-9]([0-9])?$)"         // 包含+-自然数金额格式
	MONEY3   = "(^(-)?[1-9]([0-9]{0,12})?(\\.[0-9]{1,2})?$)|(^(0){1}$)|(^(-)?[0-9]\\.[0-9]([0-9])?$)" // 包含+-的浮点数金额格式
)

Variables

View Source
var (
	CstSH, _ = time.LoadLocation("Asia/Shanghai") //上海

)
View Source
var (
	SPEL = regexp.MustCompile(`\$\{([^}]+)\}`)
)

Functions

func Add

func Add(a, b interface{}) (string, error)

func AddStr

func AddStr(input ...interface{}) string

高性能拼接字符串

func AddStrLen added in v1.1.0

func AddStrLen(length int, input ...interface{}) string

func AesCBCDecrypt added in v1.1.0

func AesCBCDecrypt(encryptedData string, key string) ([]byte, error)

AesCBCDecrypt 字符串版本的便捷方法

func AesCBCDecryptBase added in v1.1.0

func AesCBCDecryptBase(encryptedData string, key []byte) ([]byte, error)

AesCBCDecryptBase 使用指定IV的AES-CBC解密

func AesCBCEncrypt added in v1.1.0

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

AesCBCEncrypt 字符串版本的便捷方法

func AesCBCEncryptBase added in v1.1.0

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

AesCBCEncryptBase 标准AES-CBC加密,返回IV+密文的Base64编码

func AesCBCEncryptWithIV added in v1.1.0

func AesCBCEncryptWithIV(plaintext, key, iv []byte) (string, error)

AesCBCEncryptWithIV 使用指定IV的AES-CBC加密

func AesGCMDecrypt added in v1.1.0

func AesGCMDecrypt(encryptedData string, key string) ([]byte, error)

AesGCMDecrypt AES-GCM 解密(带认证验证)

func AesGCMDecryptBase added in v1.1.0

func AesGCMDecryptBase(encryptedData string, key, additionalData []byte) ([]byte, error)

AesGCMDecryptBase AES-GCM 解密基础方法 会自动验证: 1. 认证标签(AuthTag)- 确保密文未被篡改 2. 附加认证数据(AAD)- 确保关联数据未被篡改 任何一项验证失败都会返回错误,拒绝解密

func AesGCMDecryptBaseByteResult added in v1.1.0

func AesGCMDecryptBaseByteResult(encryptedData, key, additionalData []byte) ([]byte, error)

AesGCMDecryptBaseByteResult AES-GCM 解密基础方法 会自动验证: 1. 认证标签(AuthTag)- 确保密文未被篡改 2. 附加认证数据(AAD)- 确保关联数据未被篡改 任何一项验证失败都会返回错误,拒绝解密

func AesGCMDecryptWithAAD added in v1.1.0

func AesGCMDecryptWithAAD(encryptedData string, key, additionalData string) ([]byte, error)

AesGCMDecryptWithAAD AES-GCM 解密(带附加认证数据验证) additionalData: 必须与加密时使用的 AAD 完全一致

func AesGCMEncrypt added in v1.1.0

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

AesGCMEncrypt AES-GCM 加密(带认证)- 推荐用于金融/银行级应用 相比 CBC 的优势: 1. 内置完整性保护(GMAC 认证标签) 2. 防止密文篡改攻击 3. 无需 PKCS7 Padding(支持任意长度) 4. 并行加密(性能提升 2-5 倍) 5. TLS 1.3 强制使用 6. NIST/PCI DSS 优先推荐

func AesGCMEncryptBase added in v1.1.0

func AesGCMEncryptBase(plaintext, key, additionalData []byte) (string, error)

AesGCMEncryptBase AES-GCM 加密基础方法 返回格式:Base64(Nonce + Ciphertext + AuthTag) Nonce: 12 字节(GCM 标准) AuthTag: 16 字节(128-bit 认证标签)

func AesGCMEncryptBaseByteResult added in v1.1.0

func AesGCMEncryptBaseByteResult(plaintext, key, additionalData []byte) ([]byte, error)

AesGCMEncryptBaseByteResult AES-GCM 加密基础方法 返回格式:Byte(Nonce + Ciphertext + AuthTag) Nonce: 12 字节(GCM 标准) AuthTag: 16 字节(128-bit 认证标签)

func AesGCMEncryptWithAAD added in v1.1.0

func AesGCMEncryptWithAAD(plaintext []byte, key, additionalData string) (string, error)

AesGCMEncryptWithAAD AES-GCM 加密(带附加认证数据) additionalData: 不加密但需要认证的数据(如请求头、用户ID等) 使用场景:确保关联数据(如交易ID、用户ID)未被篡改

func AnyToStr

func AnyToStr(any interface{}) string

基础类型 int uint float string bool 复杂类型 json

func Base64Decode

func Base64Decode(input interface{}) []byte

default base64 - 逆向

func Base64DecodeWithPool added in v1.1.0

func Base64DecodeWithPool(input interface{}) []byte

Base64DecodeWithPool 使用缓冲池进行Base64解码的便捷函数

func Base64Encode

func Base64Encode(input interface{}) string

default base64 - 正向

func Base64EncodeWithPool added in v1.1.0

func Base64EncodeWithPool(input interface{}) string

Base64EncodeWithPool 使用缓冲池进行Base64编码的便捷函数

func Bytes2Str

func Bytes2Str(b []byte) string

Bytes2Str 零拷贝转换 []byte 为 string

⚠️ 重要警告:返回的 string 与原始 []byte 共享内存

使用场景:

  • SQL 驱动返回的 []byte(不会被修改)
  • 网络协议解析的 []byte(不会被修改)
  • 任何不会被修改的 []byte

性能:

  • 0.10 ns/op(比 string(b) 快 200x)
  • 0 B/op 零内存分配

注意:

  • 不要修改原始 []byte,否则会破坏 string 的不可变性
  • 此转换对任何 len/cap 组合都是正确的(总是读取 len 字段)
  • 此实现依赖 Go 内部 slice/string 内存布局,未来 Go 版本可能失效

示例:

b := []byte("hello")
s := Bytes2Str(b)
fmt.Println(s)  // ✅ 安全:只读访问
b[0] = 'H'      // ⚠️ 危险!会修改 s 的内容

func CheckInt

func CheckInt(c int, vs ...int) bool

检测int数值是否在区间

func CheckInt32 added in v1.0.166

func CheckInt32(c int32, vs ...int32) bool

检测int32数值是否在区间

func CheckInt64

func CheckInt64(c int64, vs ...int64) bool

检测int64数值是否在区间

func CheckLen

func CheckLen(o interface{}, min, max int) bool

获取并校验字符串长度

func CheckRangeInt added in v1.0.65

func CheckRangeInt(c, min, max int) bool

检测int数值是否在区间

func CheckRangeInt64 added in v1.0.65

func CheckRangeInt64(c, min, max int64) bool

检测int64数值是否在区间

func CheckStr

func CheckStr(c string, vs ...string) bool

检测string数值是否在区间

func CheckStrLen

func CheckStrLen(str string, min, max int) bool

获取并校验字符串长度

func ClientIP

func ClientIP(req *http.Request) string

RemoteIp 返回远程客户端的 IP,如 192.168.1.1

func Cmp

func Cmp(a, b interface{}) int

a>b=1 a=b=0 a<b=-1

func Div

func Div(a, b interface{}, n int32) (string, error)

func Error

func Error(input ...interface{}) error

高性能拼接错误对象

func FNV1a64 added in v1.1.0

func FNV1a64(s string) string

FNV1a64 快速哈希函数(非密码学安全,适合缓存键生成)

func FNV1a64Base added in v1.1.0

func FNV1a64Base(b []byte) string

FNV1a64Base 快速哈希函数(非密码学安全,适合缓存键生成)

func Float64ToInt64

func Float64ToInt64(f float64) int64

float64转int64

func FmtDiv added in v1.0.9

func FmtDiv(v string, d int64) string

func FmtEL added in v1.0.109

func FmtEL(msg string, values ...interface{}) (string, error)

func FmtZero added in v1.0.9

func FmtZero(r string) string

func GetAesIVFallback added in v1.1.0

func GetAesIVFallback(l int) []byte

GetAesIVFallback 备用IV生成方法(当crypto/rand失败时使用)

func GetAesIVSecure added in v1.1.0

func GetAesIVSecure() []byte

GetAesIVSecure 使用加密安全的随机数生成器生成IV(推荐)

func GetAesKeySecure added in v1.1.0

func GetAesKeySecure(key string) []byte

GetAesKeySecure 安全的AES密钥生成(推荐使用)

func GetAnyDayFirstAndLast

func GetAnyDayFirstAndLast(x int64) (int64, int64)

获取x天开始和结束时间,最多30天

func GetAnyMonthFirstAndLast

func GetAnyMonthFirstAndLast(month int) (int64, int64)

获取指定月份开始和结束时间

func GetBool

func GetBool(ptr uintptr) bool

get bool value

func GetBoolArr

func GetBoolArr(ptr uintptr) []bool

get []bool value

func GetBoolP added in v1.1.0

func GetBoolP(ptr uintptr) *bool

get *bool value

func GetDayFirstAndLast

func GetDayFirstAndLast() (int64, int64)

获取当天开始和结束时间

func GetFloat32

func GetFloat32(ptr uintptr) float32

get float32 value

func GetFloat32Arr

func GetFloat32Arr(ptr uintptr) []float32

get []float32 value

func GetFloat32P added in v1.0.169

func GetFloat32P(ptr uintptr) *float32

get *float32 value

func GetFloat64

func GetFloat64(ptr uintptr) float64

get float64 value

func GetFloat64Arr

func GetFloat64Arr(ptr uintptr) []float64

get []float64 value

func GetFloat64P added in v1.0.169

func GetFloat64P(ptr uintptr) *float64

get *float64 value

func GetFmtDate

func GetFmtDate(t int64) int64

获取时间的0点

func GetInDayFirstAndLast

func GetInDayFirstAndLast(x int64) (int64, int64)

获取x天开始和当天结束时间,最多30天

func GetInt

func GetInt(ptr uintptr) int

get int value

func GetInt8

func GetInt8(ptr uintptr) int8

get int8 value

func GetInt8Arr

func GetInt8Arr(ptr uintptr) []int8

get []int8 value

func GetInt8P added in v1.0.169

func GetInt8P(ptr uintptr) *int8

get *int8 value

func GetInt16

func GetInt16(ptr uintptr) int16

get int16 value

func GetInt16Arr

func GetInt16Arr(ptr uintptr) []int16

get []int16 value

func GetInt16P added in v1.0.169

func GetInt16P(ptr uintptr) *int16

get *int16 value

func GetInt32

func GetInt32(ptr uintptr) int32

get int32 value

func GetInt32Arr

func GetInt32Arr(ptr uintptr) []int32

get []int32 value

func GetInt32P added in v1.0.169

func GetInt32P(ptr uintptr) *int32

get *int32 value

func GetInt64

func GetInt64(ptr uintptr) int64

get int64 value

func GetInt64Arr

func GetInt64Arr(ptr uintptr) []int64

get []int64 value

func GetInt64P added in v1.0.169

func GetInt64P(ptr uintptr) *int64

get *int64 value

func GetIntArr

func GetIntArr(ptr uintptr) []int

get []int value

func GetIntP added in v1.0.169

func GetIntP(ptr uintptr) *int

get *int value

func GetJsonBool added in v1.0.2

func GetJsonBool(b []byte, k string) bool

func GetJsonBytes added in v1.0.102

func GetJsonBytes(b []byte, k string) []byte

func GetJsonFloat64 added in v1.0.102

func GetJsonFloat64(b []byte, k string) float64

func GetJsonInt added in v1.0.2

func GetJsonInt(b []byte, k string) int

func GetJsonInt64 added in v1.0.104

func GetJsonInt64(b []byte, k string) int64

func GetJsonObjectBytes added in v1.0.151

func GetJsonObjectBytes(b []byte, k string) []byte

func GetJsonObjectString added in v1.0.151

func GetJsonObjectString(b []byte, k string) string

func GetJsonObjectValue added in v1.0.151

func GetJsonObjectValue(b []byte) *fastjson.Value

GetJsonObjectValue 例如: v.Get("a").Get("b").MarshalTo(nil)

func GetJsonString added in v1.0.2

func GetJsonString(b []byte, k string) string

func GetLocalDynamicSecretKey added in v1.1.0

func GetLocalDynamicSecretKey() string

func GetLocalIP

func GetLocalIP() string

获取本机内网IP

func GetMonthFirstAndLast

func GetMonthFirstAndLast() (int64, int64)

获取当前月份开始和结束时间

func GetObjectID added in v1.0.29

func GetObjectID(ptr uintptr) primitive.ObjectID

get ObjectID value

func GetPath

func GetPath() string

获取项目绝对路径

func GetPtr

func GetPtr(v interface{}, offset uintptr) uintptr

通过指针获取对象字段位置

func GetRandomSecure added in v1.1.0

func GetRandomSecure(l int) []byte

GetRandomSecure 使用加密安全的随机数生成器生成指定字节数组(推荐)

func GetSnowflakeNode added in v1.0.5

func GetSnowflakeNode(n int64) *snowflake.Node

func GetString

func GetString(ptr uintptr) string

get string value

func GetStringArr

func GetStringArr(ptr uintptr) []string

get []string value

func GetStringP added in v1.0.169

func GetStringP(ptr uintptr) *string

get *string value

func GetTime added in v1.1.0

func GetTime(ptr uintptr) time.Time

get time.Time value

func GetTimeP added in v1.1.0

func GetTimeP(ptr uintptr) *time.Time

get *time.Time value

func GetUUID

func GetUUID(noHyphens bool) string

func GetUint

func GetUint(ptr uintptr) uint

get uint value

func GetUint8

func GetUint8(ptr uintptr) uint8

get uint8 value

func GetUint8Arr

func GetUint8Arr(ptr uintptr) []uint8

get []uint8 value

func GetUint8P added in v1.1.0

func GetUint8P(ptr uintptr) *uint8

get *uint8 value

func GetUint16

func GetUint16(ptr uintptr) uint16

get uint16 value

func GetUint16Arr

func GetUint16Arr(ptr uintptr) []uint16

get []uint16 value

func GetUint16P added in v1.1.0

func GetUint16P(ptr uintptr) *uint16

get *uint16 value

func GetUint32

func GetUint32(ptr uintptr) uint32

get uint32 value

func GetUint32Arr

func GetUint32Arr(ptr uintptr) []uint32

get []uint32 value

func GetUint32P added in v1.1.0

func GetUint32P(ptr uintptr) *uint32

get *uint32 value

func GetUint64

func GetUint64(ptr uintptr) uint64

get uint64 value

func GetUint64Arr

func GetUint64Arr(ptr uintptr) []uint64

get []uint64 value

func GetUint64P added in v1.1.0

func GetUint64P(ptr uintptr) *uint64

get *uint64 value

func GetUintArr

func GetUintArr(ptr uintptr) []uint

get []uint value

func GetUintP added in v1.1.0

func GetUintP(ptr uintptr) *uint

get *uint value

func GetWeekFirstAndLast

func GetWeekFirstAndLast() (int64, int64)

获取当前星期开始和结束时间

func HMAC_MD5

func HMAC_MD5(data, key string, useBase64 ...bool) string

HMAC-MD5加密

func HMAC_MD5_BASE added in v1.1.0

func HMAC_MD5_BASE(data, key []byte) []byte

HMAC_MD5_BASE 返回原始字节数组的HMAC-MD5

func HMAC_SHA1

func HMAC_SHA1(data, key string, useBase64 ...bool) string

HMAC-SHA1加密

func HMAC_SHA1_BASE added in v1.1.0

func HMAC_SHA1_BASE(data, key []byte) []byte

HMAC_SHA1_BASE 返回原始字节数组的HMAC-SHA1

func HMAC_SHA256

func HMAC_SHA256(data, key string, useBase64 ...bool) string

HMAC-SHA256加密

func HMAC_SHA256_BASE added in v1.1.0

func HMAC_SHA256_BASE(data, key []byte) []byte

HMAC_SHA256_BASE 返回原始字节数组的HMAC-SHA256

func HMAC_SHA512 added in v1.0.93

func HMAC_SHA512(data, key string, useBase64 ...bool) string

func HMAC_SHA512_BASE added in v1.1.0

func HMAC_SHA512_BASE(data, key []byte) []byte

func HasStr

func HasStr(s1 string, s2 string) bool

判定指定字符串是否存在于原字符串

func InArray

func InArray(p interface{}) []interface{}

func Int2Time

func Int2Time(t int64) time.Time

时间戳转time

func IsAccount

func IsAccount(s string) bool

func IsEmail

func IsEmail(s string) bool

func IsFloat

func IsFloat(s string) bool

func IsIDNO

func IsIDNO(s string) bool

func IsIPV4

func IsIPV4(s string) bool

func IsInt

func IsInt(s string) bool

func IsMobil

func IsMobil(s string) bool

func IsMoney

func IsMoney(s string) bool

分格式

func IsMoney2

func IsMoney2(s string) bool

常规浮点格式

func IsMoney3 added in v1.0.89

func IsMoney3(s string) bool

func IsNumber

func IsNumber(s string) bool

func IsNumber2 added in v1.0.127

func IsNumber2(s string) bool

func IsPKNO

func IsPKNO(s interface{}) bool

func IsPassword

func IsPassword(s string) bool

func IsURL

func IsURL(s string) bool

func JsonMarshal

func JsonMarshal(v interface{}) ([]byte, error)

对象转JSON字符串

func JsonToAny

func JsonToAny(src interface{}, target interface{}) error

对象转对象

func JsonUnmarshal

func JsonUnmarshal(data []byte, v interface{}) error

JSON字符串转对象

func JsonValid added in v1.0.83

func JsonValid(b []byte) bool

校验JSON格式是否合法

func JsonValidString added in v1.0.84

func JsonValidString(s string) bool

校验JSON格式是否合法

func Len

func Len(o interface{}) int

获取混合字符串实际长度

func LinuxOS added in v1.0.96

func LinuxOS() bool

func LoadYamlConfigFromPath added in v1.1.0

func LoadYamlConfigFromPath(path string) (*DIC.YamlConfig, error)

LoadYamlConfigFromPath 从指定路径读取配置文件

func LowerFirst

func LowerFirst(str string) string

65-96大写字母 97-122小写字母

func MD5

func MD5(s string, useBase64 ...bool) string

MD5加密

func MD5_BASE added in v1.1.0

func MD5_BASE(s []byte) []byte

MD5哈希

func MacOS added in v1.0.96

func MacOS() bool

func MatchFilterURL

func MatchFilterURL(requestPath string, matchPattern []string) bool

func MathAbs

func MathAbs(n int64) int64

func ModRand added in v1.0.4

func ModRand(n int) int

ModRand 调用底层生成随机数,进行取模运算,性能提升10倍

func Mul

func Mul(a, b interface{}) (string, error)

func NewDeepPassword added in v1.1.0

func NewDeepPassword(pwd, salt string) []byte

func NewFloat32

func NewFloat32(b []byte) (float32, error)

byte to float32

func NewFloat64

func NewFloat64(b []byte) (float64, error)

byte to float64

func NewInt

func NewInt(b []byte) (int, error)

byte to int

func NewInt8

func NewInt8(b []byte) (int8, error)

byte to int8

func NewInt16

func NewInt16(b []byte) (int16, error)

byte to int16

func NewInt32

func NewInt32(b []byte) (int32, error)

byte to int32

func NewInt64

func NewInt64(b []byte) (int64, error)

byte to int64

func NewPasswordBase added in v1.1.0

func NewPasswordBase(pwd, salt []byte, iter int) []byte

func NewShortPassword added in v1.1.0

func NewShortPassword(pwd, salt string) []byte

func NewString

func NewString(b []byte) (string, error)

NewString 零拷贝转换 []byte 为 string

⚠️ 重要警告:返回的 string 与原始 []byte 共享内存

使用场景:

  • SQL 驱动返回的 []byte(不会被修改)
  • 网络协议解析的 []byte(不会被修改)
  • 任何不会被修改的 []byte

性能:

  • 0.10 ns/op(比 string(b) 快 200x)
  • 0 B/op 零内存分配

注意:

  • 不要修改原始 []byte,否则会破坏 string 的不可变性
  • 此转换对任何 len/cap 组合都是正确的(总是读取 len 字段)
  • 此实现依赖 Go 内部 slice/string 内存布局,未来 Go 版本可能失效

func NewUint

func NewUint(b []byte) (uint, error)

byte to uint

func NewUint8

func NewUint8(b []byte) (uint8, error)

byte to uint8

func NewUint16

func NewUint16(b []byte) (uint16, error)

byte to uint16

func NewUint32

func NewUint32(b []byte) (uint32, error)

byte to uint32

func NewUint64

func NewUint64(b []byte) (uint64, error)

byte to uint64

func NextIID added in v1.0.5

func NextIID() int64

NextIID 获取雪花int64 ID,默认为1024区

func NextSID added in v1.0.5

func NextSID() string

NextSID 获取雪花string ID,默认为1024区

func PKCS7Padding

func PKCS7Padding(ciphertext []byte, blockSize int) []byte

func PKCS7UnPadding

func PKCS7UnPadding(plantText []byte, blockSize int) []byte

func ParseJsonBase64

func ParseJsonBase64(input interface{}, ouput interface{}) error

func RandInt

func RandInt(n int) string

func RandNonce

func RandNonce() string

func RandStr

func RandStr(n int, b ...bool) string

func ReadFile

func ReadFile(path string) ([]byte, error)

读取文件

func ReadJsonConfig

func ReadJsonConfig(conf []byte, result interface{}) error

读取JSON格式配置文件

func ReadLocalJsonConfig

func ReadLocalJsonConfig(path string, result interface{}) error

读取本地JSON配置文件

func ReadLocalYamlConfig added in v1.1.0

func ReadLocalYamlConfig(path string, result interface{}) error

读取本地YAML配置文件

func Reverse

func Reverse(s string) string

func ReverseBase64

func ReverseBase64(s string) string

func ReverseStr

func ReverseStr(s string, x ...int) string

func SHA1

func SHA1(s string, useBase64 ...bool) string

SHA1哈希

func SHA1_BASE added in v1.1.0

func SHA1_BASE(s []byte) []byte

SHA1哈希

func SHA256

func SHA256(s string, useBase64 ...bool) string

SHA256哈希

func SHA256_BASE added in v1.1.0

func SHA256_BASE(s []byte) []byte

SHA256哈希

func SHA512 added in v1.0.93

func SHA512(s string, useBase64 ...bool) string

func SHA512_BASE added in v1.1.0

func SHA512_BASE(s []byte) []byte

SHA512哈希

func SetBool

func SetBool(ptr uintptr, v bool)

set bool value

func SetBoolArr

func SetBoolArr(ptr uintptr, v []bool)

set []bool value

func SetBoolP added in v1.1.0

func SetBoolP(ptr uintptr, v *bool)

set *bool value

func SetDynamicSecretKey added in v1.1.3

func SetDynamicSecretKey(key string)

func SetFloat32

func SetFloat32(ptr uintptr, v float32)

set float32 value

func SetFloat32Arr

func SetFloat32Arr(ptr uintptr, v []float32)

set []float32 value

func SetFloat32P added in v1.0.169

func SetFloat32P(ptr uintptr, v *float32)

set *float32 value

func SetFloat64

func SetFloat64(ptr uintptr, v float64)

set float64 value

func SetFloat64Arr

func SetFloat64Arr(ptr uintptr, v []float64)

set []float64 value

func SetFloat64P added in v1.0.169

func SetFloat64P(ptr uintptr, v *float64)

set *float64 value

func SetInt

func SetInt(ptr uintptr, v int)

set int value

func SetInt8

func SetInt8(ptr uintptr, v int8)

set int8 value

func SetInt8Arr

func SetInt8Arr(ptr uintptr, v []int8)

set []int8 value

func SetInt8P added in v1.0.169

func SetInt8P(ptr uintptr, v *int8)

set *int8 value

func SetInt16

func SetInt16(ptr uintptr, v int16)

set int16 value

func SetInt16Arr

func SetInt16Arr(ptr uintptr, v []int16)

set []int16 value

func SetInt16P added in v1.0.169

func SetInt16P(ptr uintptr, v *int16)

set *int16 value

func SetInt32

func SetInt32(ptr uintptr, v int32)

set int32 value

func SetInt32Arr

func SetInt32Arr(ptr uintptr, v []int32)

set []int32 value

func SetInt32P added in v1.0.169

func SetInt32P(ptr uintptr, v *int32)

set *int32 value

func SetInt64

func SetInt64(ptr uintptr, v int64)

set int64 value

func SetInt64Arr

func SetInt64Arr(ptr uintptr, v []int64)

set []int64 value

func SetInt64P added in v1.0.169

func SetInt64P(ptr uintptr, v *int64)

set *int64 value

func SetIntArr

func SetIntArr(ptr uintptr, v []int)

set []int value

func SetIntP added in v1.0.169

func SetIntP(ptr uintptr, v *int)

set *int value

func SetObjectID added in v1.0.29

func SetObjectID(ptr uintptr, v primitive.ObjectID)

set ObjectID value

func SetString

func SetString(ptr uintptr, v string)

set string value

func SetStringArr

func SetStringArr(ptr uintptr, v []string)

set []string value

func SetStringP added in v1.0.169

func SetStringP(ptr uintptr, v *string)

set *string value

func SetTime added in v1.1.0

func SetTime(ptr uintptr, v time.Time)

set time.Time value

func SetTimeP added in v1.1.0

func SetTimeP(ptr uintptr, v *time.Time)

set *time.Time value

func SetUint

func SetUint(ptr uintptr, v uint)

set uint value

func SetUint8

func SetUint8(ptr uintptr, v uint8)

set uint8 value

func SetUint8Arr

func SetUint8Arr(ptr uintptr, v []uint8)

set []uint8 value

func SetUint8P added in v1.1.0

func SetUint8P(ptr uintptr, v *uint8)

set *uint8 value

func SetUint16

func SetUint16(ptr uintptr, v uint16)

set uint16 value

func SetUint16Arr

func SetUint16Arr(ptr uintptr, v []uint16)

set []uint16 value

func SetUint16P added in v1.1.0

func SetUint16P(ptr uintptr, v *uint16)

set *uint16 value

func SetUint32

func SetUint32(ptr uintptr, v uint32)

set uint32 value

func SetUint32Arr

func SetUint32Arr(ptr uintptr, v []uint32)

set []uint32 value

func SetUint32P added in v1.1.0

func SetUint32P(ptr uintptr, v *uint32)

set *uint32 value

func SetUint64

func SetUint64(ptr uintptr, v uint64)

set uint64 value

func SetUint64Arr

func SetUint64Arr(ptr uintptr, v []uint64)

set []uint64 value

func SetUint64P added in v1.1.0

func SetUint64P(ptr uintptr, v *uint64)

set *uint64 value

func SetUintArr

func SetUintArr(ptr uintptr, v []uint)

set []uint value

func SetUintP added in v1.1.0

func SetUintP(ptr uintptr, v *uint)

set *uint value

func Shift

func Shift(input interface{}, ln int, fz bool) string

保留小数位

func ShiftN

func ShiftN(a interface{}, n int32) (string, error)

func StartWait

func StartWait(msg string)

无限等待

func Str2Bytes

func Str2Bytes(s string) []byte

Str2Bytes 零拷贝转换 string 为 []byte

⚠️ 重要警告:返回的 []byte 与原始 string 共享内存

使用场景:

  • 只读访问(哈希计算、HMAC、加密等)
  • 临时传参(不会被修改的场景)
  • 性能关键路径(高频调用)

性能:

  • 0.10 ns/op(比 []byte(s) 快 200x)
  • 0 B/op 零内存分配

注意:

  • string 在 Go 中是不可变的(immutable),底层数据存储在只读内存段
  • 尝试修改返回的 []byte 会触发运行时 panic(写入只读内存)
  • 返回的 []byte 保证 len == cap
  • 此实现依赖 Go 内部 string/slice 内存布局,未来版本可能失效

示例:

s := "hello"
b := Str2Bytes(s)
hash := md5.Sum(b)  // ✅ 安全:只读访问
b[0] = 'H'          // ⚠️ 运行时 panic:尝试写入只读内存

func Str2Date

func Str2Date(s string) (int64, error)

格式字符串转时间戳/毫秒

func Str2FormatTime added in v1.0.161

func Str2FormatTime(s string, fmt string, local *time.Location) (int64, error)

格式字符串转时间戳/毫秒

func Str2Time

func Str2Time(s string) (int64, error)

格式字符串转时间戳/毫秒 2023-07-22 08:47:27.379 2023-07-22 08:47:27 2023-07-22

func StrToBool

func StrToBool(str string) (bool, error)

string to bool

func StrToFloat

func StrToFloat(str string) (float64, error)

转换成小数

func StrToInt

func StrToInt(str string) (int, error)

string to int

func StrToInt8

func StrToInt8(str string) (int8, error)

string to int8

func StrToInt16

func StrToInt16(str string) (int16, error)

string to int16

func StrToInt32

func StrToInt32(str string) (int32, error)

string to int32

func StrToInt64

func StrToInt64(str string) (int64, error)

string to int64

func Sub

func Sub(a, b interface{}) (string, error)

func Substr

func Substr(str string, start int, end int) string

截取字符串 start 起点下标 end 结束下标

func Time2DateStr

func Time2DateStr(t int64) string

时间戳转格式字符串/毫秒

func Time2FormatStr added in v1.0.160

func Time2FormatStr(t int64, fmt string, local *time.Location) string

时间戳转格式字符串/毫秒

func Time2Str

func Time2Str(t int64) string

时间戳转格式字符串/毫秒, 例: 2023-07-22 08:47:27.379

func ToJsonBase64

func ToJsonBase64(input interface{}) (string, error)

func UnixMilli added in v1.0.5

func UnixMilli() int64

获取当前时间/毫秒

func UnixNano added in v1.0.4

func UnixNano() int64

UnixNano 直接调用底层方法比time.Now()性能提升1倍

func UnixSecond added in v1.0.4

func UnixSecond() int64

获取当前时间/秒

func UpperFirst

func UpperFirst(str string) string

65-96大写字母 97-122小写字母

func ValidPattern

func ValidPattern(content, pattern string) bool

func WindowsOS added in v1.0.96

func WindowsOS() bool

Types

type Base64Pool added in v1.1.0

type Base64Pool struct {
	// contains filtered or unexported fields
}

Base64Pool Base64编解码缓冲池

func GetBase64Pool added in v1.1.0

func GetBase64Pool() *Base64Pool

GetBase64Pool 获取全局Base64池子实例

func (*Base64Pool) Decode added in v1.1.0

func (p *Base64Pool) Decode(input interface{}) []byte

Decode 使用缓冲池进行Base64解码

func (*Base64Pool) Encode added in v1.1.0

func (p *Base64Pool) Encode(input interface{}) string

Encode 使用缓冲池进行Base64编码

Directories

Path Synopsis
Package decimal implements an arbitrary precision fixed-point decimal.
Package decimal implements an arbitrary precision fixed-point decimal.
Package snowflake provides a very simple Twitter snowflake generator and parser.
Package snowflake provides a very simple Twitter snowflake generator and parser.

Jump to

Keyboard shortcuts

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