util

package
v1.0.14 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2026 License: Apache-2.0 Imports: 30 Imported by: 1

Documentation

Index

Constants

View Source
const (
	// Alphanumeric 包含大小写字母和数字
	Alphanumeric = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
	// Lowercase 仅包含小写字母
	Lowercase = "abcdefghijklmnopqrstuvwxyz"
	// Uppercase 仅包含大写字母
	Uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	// Numeric 仅包含数字
	Numeric = "0123456789"
	// WithSpecialChars 包含字母、数字和常见特殊字符
	WithSpecialChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()"
)

预设字符集

View Source
const DefaultChunkSize = 4096 // 4KB

DefaultChunkSize 常用分块大小:4KB,与大多数操作系统页大小一致

View Source
const DefaultPageNo = 1
View Source
const DefaultPageSize = 10

Variables

View Source
var DateFormat = "2006-01-02"
View Source
var TimestampFormat = "2006-01-02 15:04:05"

Functions

func AbsPath added in v0.9.3

func AbsPath(relativePath string) (string, error)

AbsPath 将相对路径转为绝对路径,基于当前工作目录解析

func AddQueryParam added in v0.6.1

func AddQueryParam(url, key, value string) string

AddQueryParam adds a query parameter to a URL string. It automatically detects whether the URL already has query parameters and uses the appropriate separator (? or &).

Example:

AddQueryParam("/path", "key", "value") -> "/path?key=value"
AddQueryParam("/path?foo=bar", "key", "value") -> "/path?foo=bar&key=value"

func AddQueryParamFlag added in v0.6.1

func AddQueryParamFlag(url, key string) string

AddQueryParamFlag adds a boolean flag query parameter (key=1) to a URL string.

func AddQueryParamInt added in v0.6.1

func AddQueryParamInt(url, key string, value int) string

AddQueryParamInt adds an integer query parameter to a URL string.

func AnyArrayContainsIgnoreCase added in v0.1.2

func AnyArrayContainsIgnoreCase(s []string, strs ...string) bool

AnyArrayContainsIgnoreCase 检查目标字符串切片 s 中是否存在任意一个字符串,忽略大小写地包含任意一个待匹配字符串 strs 参数 s: 源字符串切片 参数 strs: 可变参数,待匹配的一个或多个子字符串 返回值: 只要 s 中有一个字符串包含任意一个 strs 中的子字符串(忽略大小写),返回 true;否则返回 false

func AnyContainsIgnoreCase added in v0.1.2

func AnyContainsIgnoreCase(s string, strs ...string) bool

func ArrayIntContains added in v0.1.1

func ArrayIntContains(arr []int, str int) bool

func BoolToString

func BoolToString(b bool) string

func CRC added in v0.1.2

func CRC(len int, key string) string

func CalculateMD5

func CalculateMD5(data []byte) (string, error)

CalculateMD5 计算字节数组的MD5哈希值(自动分块处理)

func CalculateMD5WithChunkSize

func CalculateMD5WithChunkSize(data []byte, chunkSize int) (string, error)

CalculateMD5WithChunkSize 支持自定义分块大小的MD5计算 适合处理超大字节数组,避免一次性加载全部数据

func CommonCharCount added in v0.9.1

func CommonCharCount(text1, text2 string) int

CommonCharCount 计算两个文本中相同字符的数量(每个字符只计一次,去重计数) 返回值是相同独立字符的个数

func CommonCharSimilarity added in v0.9.1

func CommonCharSimilarity(text1, text2 string) int

CommonCharSimilarity 基于 CommonCharCount 计算相似度(0-100) 得分 = 相同去重字符数 / max(len(text1), len(text2)) * 100

func ContainsAny

func ContainsAny(s string, strs ...string) bool

func ContainsAnyIgnoreCase

func ContainsAnyIgnoreCase(s string, strs ...string) bool

func ContainsAnyIgnorePunctuation added in v0.7.1

func ContainsAnyIgnorePunctuation(s string, strs ...string) bool

func CreateDirIfNoExists added in v0.0.2

func CreateDirIfNoExists(path string) error

func CreateFileIfNoExists added in v0.1.2

func CreateFileIfNoExists(path string) error

func DaysBetween added in v0.1.3

func DaysBetween(t1 time.Time, t2 time.Time) int

func DecodeBase64URL added in v0.6.1

func DecodeBase64URL(encoded string) (string, error)

DecodeBase64URL 解码 base64 URL 编码的字符串(自动处理 padding)

func DecodeFileBase64

func DecodeFileBase64(base64Str string) ([]byte, error)

DecodeBase64 解码base64字符串为字节数组

func DecryptByCBC

func DecryptByCBC(cipherText string, key string, iv string) (string, error)

DecryptByCBC AES-256-CBC 解密实现

func DecryptCBCBase64 added in v0.6.1

func DecryptCBCBase64(key, iv []byte, cipherTextBase64 string) ([]byte, error)

DecryptCBCBase64 AES-256-CBC 解密(Base64密文版本) key: 32字节密钥 iv: 16字节IV(如果为nil则使用key的前16字节) cipherTextBase64: Base64编码的密文 返回:解密后的明文

func DecryptCBCBytes added in v0.6.1

func DecryptCBCBytes(key, iv, ciphertext []byte) ([]byte, error)

DecryptCBCBytes AES-256-CBC 解密(字节数组版本) key: 32字节密钥 iv: 16字节IV(如果为nil则使用key的前16字节) ciphertext: 密文 返回:解密后的明文

func DeduplicateIds

func DeduplicateIds(ids string) string

func EncryptByCBC

func EncryptByCBC(text string, key string, iv string) (string, error)

EncryptByCBC AES-256-CBC 加密实现

func EqualsAnyIgnoreCase

func EqualsAnyIgnoreCase(s string, strs ...string) bool

func EqualsAnyIgnorePunctuation added in v0.6.1

func EqualsAnyIgnorePunctuation(s string, strs ...string) bool

EqualsAnyIgnorePunctuation 比较字符串是否相等(忽略标点符号)

func EqualsAnyIgnorePunctuationAndCase added in v0.6.1

func EqualsAnyIgnorePunctuationAndCase(s string, strs ...string) bool

EqualsAnyIgnorePunctuationAndCase 比较字符串是否相等(忽略标点符号和大小写)

func ExecDir added in v0.9.3

func ExecDir() (string, error)

ExecDir 返回当前可执行文件所在目录的绝对路径

func ExistsFile added in v0.0.2

func ExistsFile(path string) bool

ExistsFile 判断指定路径是否为**存在的文件**(非目录、非不存在) 返回值:true=文件存在;false=文件不存在/是目录/其他错误

func ExtractEmails added in v0.1.2

func ExtractEmails(text string) []string

func FormatDate

func FormatDate(tm time.Time) string

func FormatMail added in v0.1.2

func FormatMail(name, mail string) string

func FormatTime

func FormatTime(tm time.Time) string

func GenerateRandomNum

func GenerateRandomNum(length int) string

func GenerateRandomString

func GenerateRandomString(length int, charset string) string

GenerateRandomString 生成指定长度的随机字符串 length: 字符串长度 charset: 字符集,如果为空则使用默认的字母数字字符集

func GenerateRandomStringByAlphanumeric

func GenerateRandomStringByAlphanumeric(length int) string

func GetCachePath

func GetCachePath(rootPath, filename string) string

func GetDomainFromHost

func GetDomainFromHost(host string) string

func GetDomainFromURL

func GetDomainFromURL(rawURL string) string

func GetEnv added in v0.1.2

func GetEnv(key string) string

func GetEnvBool added in v0.1.2

func GetEnvBool(key string) bool

func GetEnvBoolOrDefault added in v0.1.2

func GetEnvBoolOrDefault(key string, defaultValue bool) bool

func GetEnvInt added in v0.1.2

func GetEnvInt(key string) int

func GetEnvIntOrDefault added in v0.1.2

func GetEnvIntOrDefault(key string, defaultValue int) int

func GetEnvOrDefault added in v0.1.2

func GetEnvOrDefault(key, defaultValue string) string

func GetHost

func GetHost(rawURL string) string

func GetMap

func GetMap(key string, value any) map[string]any

func GetNowTime

func GetNowTime() time.Time

func GetStructFullName added in v0.1.2

func GetStructFullName(v any) string

func GetStructFullQualifiedName added in v0.5.1

func GetStructFullQualifiedName(v any) string

GetStructFullQualifiedName returns the fully qualified name of the struct including full package path (e.g. "github.com/chuccp/antilost_qrcode_go/rest.User")

func GetStructName added in v0.1.2

func GetStructName(v any) string

func GetStructPkgPath added in v0.5.1

func GetStructPkgPath(v any) string

GetStructPkgPath returns the full package path of the struct (e.g. "github.com/chuccp/antilost_qrcode_go/rest")

func HidePhone added in v0.1.2

func HidePhone(phone string) string

func Index2Str

func Index2Str(index int, charset string) string

func IsAfter added in v0.1.2

func IsAfter(pre string, now time.Time, timestampFormat string) (bool, error)

func IsAfterTime added in v0.1.3

func IsAfterTime(pre string, now time.Time) (bool, error)

func IsBlank

func IsBlank(str string) bool

func IsDomain added in v0.1.1

func IsDomain(str string) bool

func IsImagePath added in v0.1.2

func IsImagePath(path string) bool

IsImageByURL 通过URL路径判断是否为图片 参数:rawURL 原始URL字符串(如"https://example.com/photo.jpg?width=100") 返回:bool(是否为图片)、error(URL解析错误)

func IsMatch

func IsMatch(value, regx string) bool

func IsMatchPath

func IsMatchPath(path, smath string) bool

func IsNotBlank

func IsNotBlank(str string) bool

func IsNumber

func IsNumber(value string) bool

func JoinUrl added in v0.6.1

func JoinUrl(root string, args ...string) string

JoinUrl joins URL path segments into a single URL string. It properly handles leading and trailing slashes between segments. It also normalizes backslashes to forward slashes (useful for Windows paths).

Example:

JoinUrl("http://example.com", "api", "users") -> "http://example.com/api/users"
JoinUrl("/api", "users", "1") -> "/api/users/1"
JoinUrl("http://example.com/", "/api/", "/users/") -> "http://example.com/api/users"
JoinUrl("C:\\static\\voice", "58\\file.mp3") -> "C:/static/voice/58/file.mp3"

func JoinValues added in v0.0.4

func JoinValues(values ...any) string

func JsonEncode added in v0.1.2

func JsonEncode(v any) (string, error)

func MD5

func MD5(data []byte) string

func MD5Str

func MD5Str(data string) string

func Millisecond

func Millisecond() uint32

func NewPtr

func NewPtr[T any](v T) T

func NewSlice

func NewSlice[T any](v T) []T

func NowDateFormatTime added in v0.1.2

func NowDateFormatTime(timestampFormat string) string

func NowDateTime

func NowDateTime() string

func Number2String

func Number2String(number int64, charset string) string

func OfMap

func OfMap(key string, value any) map[string]any

func OfMap2 added in v0.1.2

func OfMap2(key string, value any, key2 string, value2 any) map[string]any

func PKCS7Pad added in v0.6.1

func PKCS7Pad(data []byte, blockSize int) []byte

PKCS7Pad 添加 PKCS7 填充

func PKCS7Unpad added in v0.6.1

func PKCS7Unpad(data []byte) ([]byte, error)

PKCS7Unpad 去除 PKCS7 填充

func ParseFormatTime added in v0.1.2

func ParseFormatTime(timeStr, timestampFormat string) (time.Time, error)

func ParseMail added in v0.1.2

func ParseMail(recipient string) (name string, mail string, err error)

func ParseTemplate added in v0.1.1

func ParseTemplate(templateStr string, data map[string]interface{}) (string, error)

func ReadFile

func ReadFile(path string) (string, error)

func ReadFileBytes

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

func RemoveDuplicates

func RemoveDuplicates(nums []string) []string

func RemovePunctuation added in v0.6.1

func RemovePunctuation(s string) string

RemovePunctuation 移除字符串中的标点符号和特殊符号(保留中文、英文、数字)

func ReplaceAllRegex

func ReplaceAllRegex(path, regex, math string) string

func SHA1 added in v0.6.1

func SHA1(str string) string

SHA1 计算字符串的 SHA1 哈希值并返回十六进制编码结果

func SHA1Bytes added in v0.6.1

func SHA1Bytes(data []byte) string

SHA1Bytes 计算字节数组的 SHA1 哈希值并返回十六进制编码结果

func Second

func Second() int64

func Second2Base62

func Second2Base62() string

func SplitAndDeduplicate

func SplitAndDeduplicate(text, sep string) []string

func SplitPath

func SplitPath(path string) []string

func StartsWithAny added in v0.2.1

func StartsWithAny(s string, prefix ...string) bool

func StartsWithAnyIgnoreCase added in v0.0.4

func StartsWithAnyIgnoreCase(s string, prefix ...string) bool

func String2Number added in v0.9.3

func String2Number(s string, charset string) int64

String2Number 将指定字符集编码的字符串解码为数字,是 Number2String 的逆操作

func StringToBool

func StringToBool(s string) bool

func StringToInt

func StringToInt(s string) int

func StringToUInt

func StringToUInt(s string) uint

func StringToUintIds

func StringToUintIds(ids string) []uint

func SubStringAndPadSpace added in v0.1.2

func SubStringAndPadSpace(value string, length int) string

SubStringAndPadSpace 截取字符串到指定长度,若原字符串长度不足则右侧补空格 参数:

value - 原始字符串
length - 目标字符串长度

返回值:

截取/填充后的指定长度字符串

func SubStringLastMaxLength added in v0.1.3

func SubStringLastMaxLength(value string, maxLength int) string

func SubStringMaxLength added in v0.1.3

func SubStringMaxLength(value string, maxLength int) string

func TextSimilarity added in v0.7.1

func TextSimilarity(text1, text2 string) int

TextSimilarity 计算两个文本的相似度得分(0-100) 使用基于公共子串的相似度算法

func Trim

func Trim(value string) string

func WriteBase64File

func WriteBase64File(base64Str string, dst string) error

func WriteBytesFile added in v0.0.2

func WriteBytesFile(file *os.File, dataS ...[]byte) error

func WriteBytesFilePath added in v0.0.2

func WriteBytesFilePath(path string, dataS ...[]byte) error

func WriteFile

func WriteFile(bytes []byte, dst string) error

Types

type Configure

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

type File added in v0.0.2

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

func GetRootPath added in v0.0.2

func GetRootPath() ([]*File, error)

func NewFile added in v0.0.2

func NewFile(path string) (*File, error)

func (*File) Abs added in v0.0.2

func (f *File) Abs() string

func (*File) Child added in v0.0.2

func (f *File) Child(path string) (*File, error)

func (*File) Close added in v0.0.2

func (f *File) Close() error

func (*File) Exists added in v0.0.2

func (f *File) Exists() (flag bool, err error)

func (*File) IsDir added in v0.0.2

func (f *File) IsDir() bool

func (*File) IsDisk added in v0.0.2

func (f *File) IsDisk() bool

func (*File) List added in v0.0.2

func (f *File) List() ([]*File, error)

func (*File) MkDirs added in v0.0.2

func (f *File) MkDirs() error

func (*File) ModTime added in v0.0.2

func (f *File) ModTime() (*time.Time, error)

func (*File) Name added in v0.0.2

func (f *File) Name() string

func (*File) OpenAppendOrCreate added in v0.0.2

func (f *File) OpenAppendOrCreate() error

func (*File) OpenAppendWrite added in v0.0.2

func (f *File) OpenAppendWrite() (*bufio.Writer, error)

func (*File) OpenOrCreate added in v0.0.2

func (f *File) OpenOrCreate() error

func (*File) OpenWrite added in v0.0.2

func (f *File) OpenWrite() (*bufio.Writer, error)

func (*File) Parent added in v0.0.2

func (f *File) Parent() string

func (*File) ParentFile added in v0.0.2

func (f *File) ParentFile() (*File, error)

func (*File) ReadAll added in v0.0.2

func (f *File) ReadAll() ([]byte, error)

func (*File) ReadBytes added in v0.0.2

func (f *File) ReadBytes(p []byte) (n int, err error)

func (*File) ToRawFile added in v0.0.2

func (f *File) ToRawFile() (*os.File, error)

func (*File) Truncate added in v0.0.2

func (f *File) Truncate() error

func (*File) WriteAppendBytes added in v0.0.2

func (f *File) WriteAppendBytes(data []byte) error

func (*File) WriteBytes added in v0.0.2

func (f *File) WriteBytes(data []byte) error

type Page added in v1.0.4

type Page struct {
	PageNo   int // Current page number, 1-based
	PageSize int // Number of items per page
	LastId   int // Last seen ID for cursor-based pagination
}

Page represents pagination parameters for list queries.

func DefaultPage added in v1.0.12

func DefaultPage(page *Page) *Page

type PageAble added in v1.0.4

type PageAble[T any] struct {
	Total int64 `json:"total"` // Total number of items
	List  []T   `json:"list"`  // Items on the current page
}

PageAble is a paginated response wrapper containing total count and item list.

func ToPage added in v1.0.4

func ToPage[T any](total int64, list []T) *PageAble[T]

ToPage creates a new PageAble from the given total count and item list.

Jump to

Keyboard shortcuts

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