Documentation
¶
Index ¶
- Constants
- Variables
- func AppendStringToFile(filename, content string) error
- func AssignGroup(id uint32, seed uint32) uint32
- func Base64ToString(s string) (string, error)
- func CalSwitchRemain(lastTime int, cd int) int
- func CheckPasswordHash(password, hash string) bool
- func Compare(v1, v2 string) int
- func CopyFile(src, dst string) error
- func CreateDirIfNotExist(path string) error
- func Difference[T ElementType](slice1, slice2 []T) []T
- func DirExists(path string) bool
- func FileExists(filename string) bool
- func FindFilesByExt(dirPath string, ext string) ([]string, error)
- func FloatRatioToInt(input []float32) []int
- func GenerateToken(config *JWTConfig, userID uint, username string) (string, error)
- func GetConstellation(timestamp int64) uint
- func GetFileSize(filename string) (int64, error)
- func GetJSON(url string, v interface{}) error
- func GetUtilityTime() *utilityTime
- func HashPassword(password string) (string, error)
- func InArray[T ElementType](target T, arr []T) bool
- func IsEmptyDir(dirPath string) (bool, error)
- func IsMultibyte(r rune) bool
- func ListFilesInDir(dirPath string) ([]string, error)
- func ListToMap[T any, K comparable](list []T, keySelector func(T) K) map[K]T
- func ListToMapByField[T any](list []T, key string) map[int]T
- func Lottery(probabilities []int) int
- func MaskNickname(nickname string) string
- func MergeMaps[K comparable, V any](maps ...map[K]V) map[K]V
- func PostJSON(url string, body, v interface{}) error
- func RandInt(nums []int, dayN uint, needNum int) []int
- func RandomIdx(num int) int
- func RandomReadableUniCode6Len() string
- func RandomReadableUniCode8Len() string
- func RandomStrictUniCode8Len() string
- func RandomString(n int) string
- func RandomUniCode(longCode bool, readability bool) (string, error)
- func ReadFileToString(filename string) (string, error)
- func ReadJSONFile(filename string, v interface{}) error
- func ReadLines(filename string) ([]string, error)
- func RefreshToken(config *JWTConfig, tokenString string) (string, error)
- func SafeRemove(path string) error
- func SampleGenerateCode(codeLength int) (string, error)
- func StringCamelToPascal(s string) string
- func StringCamelToSnake(s string) string
- func StringContainsAll(s string, substrings ...string) bool
- func StringContainsAny(s string, substrings ...string) bool
- func StringCountWords(s string) int
- func StringExtractAllByRegex(s, pattern string) ([][]string, error)
- func StringExtractByRegex(s, pattern string) ([]string, error)
- func StringFormatByteSize(bytes int64) string
- func StringIsEmpty(s string) bool
- func StringIsMatch(s, pattern string) (bool, error)
- func StringIsNotEmpty(s string) bool
- func StringJoinWithSeparator(sep string, parts ...string) string
- func StringMaskEmail(email string) string
- func StringMaskPhone(phone string) string
- func StringPascalToCamel(s string) string
- func StringRemoveDuplicates(slice []string) []string
- func StringReplaceAll(s string, replacements map[string]string) string
- func StringReverse(s string) string
- func StringSnakeToCamel(s string) string
- func StringSplitAndTrim(s, sep string) []string
- func StringTemplate(templateStr string, data interface{}) (string, error)
- func StringToBase64(s string) string
- func StringToMD5(s string) string
- func StringToSHA1(s string) string
- func StringToSHA256(s string) string
- func StringTruncate(s string, maxLen int) string
- func TempFile(dir, pattern string) (*os.File, error)
- func UniInvCodeLen6ByUID(uid uint64, baseChars []byte) (string, error)
- func UniqueElements[T ElementType](elements []T) []T
- func ValidateToken(config *JWTConfig, tokenString string) bool
- func VerCompare(v1, v2, operator string) bool
- func WalkDir(dirPath string, callback func(path string, info os.FileInfo) error) error
- func WriteJSONFile(filename string, v interface{}, pretty bool) error
- func WriteLines(filename string, lines []string) error
- func WriteStringToFile(filename, content string) error
- type ElementType
- type HTTPClient
- type HTTPRequest
- func (r *HTTPRequest) Send() *HTTPResponse
- func (r *HTTPRequest) SetBody(body interface{}) *HTTPRequest
- func (r *HTTPRequest) SetClient(client HTTPClient) *HTTPRequest
- func (r *HTTPRequest) SetContext(ctx context.Context) *HTTPRequest
- func (r *HTTPRequest) SetHeader(key, value string) *HTTPRequest
- func (r *HTTPRequest) SetHeaders(headers map[string]string) *HTTPRequest
- func (r *HTTPRequest) SetMethod(method string) *HTTPRequest
- func (r *HTTPRequest) SetQueryParam(key, value string) *HTTPRequest
- func (r *HTTPRequest) SetQueryParams(params map[string]string) *HTTPRequest
- func (r *HTTPRequest) SetTimeout(timeout time.Duration) *HTTPRequest
- func (r *HTTPRequest) SetURL(url string) *HTTPRequest
- type HTTPResponse
- type IpDetail
- type JWTClaims
- type JWTConfig
- type SyncMap
Constants ¶
const ( InvCodePrime1 = 3 // 与字符集长度 62 互质 InvCodePrime2 = 5 // 与邀请码长度 6 互质 InvCodeSalt = 4213098675 // 随意取一个数值 )
Variables ¶
var ( ErrInvalidBase64 = errors.New("invalid base64 string") ErrInvalidRegex = errors.New("invalid regular expression") )
Functions ¶
func AppendStringToFile ¶
AppendStringToFile 将字符串追加到文件末尾
func Base64ToString ¶
Base64ToString decodes a Base64 string
func CalSwitchRemain ¶
func CheckPasswordHash ¶
CheckPasswordHash 校验密码与 hash
func Compare ¶
Compare 比较格式为 X.Y.Z 的版本号的大小关系(X、Y、Z 为纯数字) 返回值:0 表示v1与v2相等;1 表示v1大于v2;2 表示v1小于v2;-1 表示版本号格式错误
func CreateDirIfNotExist ¶
CreateDirIfNotExist 如果目录不存在则创建
func FindFilesByExt ¶
FindFilesByExt 查找指定目录下所有具有特定扩展名的文件
func FloatRatioToInt ¶
FloatRatioToInt 将浮点数数组转换为整数百分比(确保总和 100)
func GenerateToken ¶
GenerateToken 生成 JWT token
func GetConstellation ¶
func GetUtilityTime ¶
func GetUtilityTime() *utilityTime
func ListFilesInDir ¶
ListFilesInDir 列出目录中的所有文件(不包括子目录)
func ListToMap ¶
func ListToMap[T any, K comparable](list []T, keySelector func(T) K) map[K]T
ListToMap 泛型方法,允许自定义 key 选择
func ListToMapByField ¶
ListToMapByField 反射版 ListToMap,支持结构体字段作为 key
func MaskNickname ¶
MaskNickname 隐藏昵称 - 单字节字符(如英文、数字)显示前两位,其余用星号替换 - 多字节字符(如中文、日文、韩文)显示第一位,其余用星号替换
func MergeMaps ¶
func MergeMaps[K comparable, V any](maps ...map[K]V) map[K]V
MergeMaps 泛型合并 map,适用于所有类型的 key 和 value
func RandomReadableUniCode6Len ¶
func RandomReadableUniCode6Len() string
RandomReadableUniCode6Len 返回可读性高的 6 位唯一码
func RandomReadableUniCode8Len ¶
func RandomReadableUniCode8Len() string
RandomReadableUniCode8Len 返回可读性高的 8 位唯一码
func RandomStrictUniCode8Len ¶
func RandomStrictUniCode8Len() string
RandomStrictUniCode8Len 返回严格不重复的 8 位唯一码
func RandomString ¶
RandomString 随机生成指定长度的十六进制字符串 输入长度 n 会被转换为最接近的偶数(向下取整) 例如:输入1或2返回2位字符,输入3或4返回4位字符 RandomString 随机生成 n 位字符串
func RandomUniCode ¶
RandomUniCode 随机生成 6 或 8 位不重复(非严格意义)字符串 longCode 为 true 时返回 8 位长度;false 时返回 6 位 readability 为 true 时返回只包含数字和大写字母(排除 0、O、1、I);为 false 时返回可能包含所有数字和大小写字母
重复概率参考数据: [longCode == false && readability == true]
- 连续生成 10 万次,约 5 次重复;
- 连续生成 100 万次,约 500 次重复;
- 连续生成 1000 万次,约 50000 次重复
[longCode == false && readability == false]
- 连续生成 10 万次,约 <1 次重复;
- 连续生成 100 万次,约 10 次重复;
- 连续生成 1000 万次,约 1000 次重复
[longCode == true && readability == true]
- 连续生成 10 万次,约 <1 次重复;
- 连续生成 100 万次,约 <1 次重复;
- 连续生成 1000 万次,约 <50 次重复
[longCode == true && readability == false]
- 理论上不重复(与 uuid 一一对应)
func ReadFileToString ¶
ReadFileToString 安全地读取文件内容到字符串
func ReadJSONFile ¶
ReadJSONFile 从JSON文件读取数据到结构体
func RefreshToken ¶
RefreshToken 刷新 JWT token
func SampleGenerateCode ¶
func StringCamelToPascal ¶
StringCamelToPascal converts a camelCase string to PascalCase
func StringCamelToSnake ¶
StringCamelToSnake converts a camelCase string to snake_case
func StringContainsAll ¶
StringContainsAll checks if a string contains all of the provided substrings
func StringContainsAny ¶
StringContainsAny checks if a string contains any of the provided substrings
func StringCountWords ¶
StringCountWords counts the number of words in a string
func StringExtractAllByRegex ¶
StringExtractAllByRegex extracts all matches from a string using a regular expression
func StringExtractByRegex ¶
StringExtractByRegex extracts content from a string using a regular expression
func StringFormatByteSize ¶
StringFormatByteSize formats a byte size into a human-readable string
func StringIsEmpty ¶
StringIsEmpty checks if a string is empty or contains only whitespace
func StringIsMatch ¶
StringIsMatch checks if a string matches the provided regular expression pattern
func StringIsNotEmpty ¶
StringIsNotEmpty checks if a string is not empty and contains non-whitespace characters
func StringJoinWithSeparator ¶
StringJoinWithSeparator joins strings with a separator, ignoring empty strings
func StringMaskEmail ¶
StringMaskEmail masks an email address for privacy
func StringMaskPhone ¶
StringMaskPhone masks a phone number for privacy
func StringPascalToCamel ¶
StringPascalToCamel converts a PascalCase string to camelCase
func StringRemoveDuplicates ¶
StringRemoveDuplicates removes duplicate strings from a slice while preserving order
func StringReplaceAll ¶
StringReplaceAll replaces multiple substrings in a string according to the provided map
func StringReverse ¶
StringReverse reverses a string while preserving UTF-8 encoding
func StringSnakeToCamel ¶
StringSnakeToCamel converts a snake_case string to camelCase
func StringSplitAndTrim ¶
StringSplitAndTrim splits a string by the separator and trims whitespace from each part
func StringTemplate ¶
StringTemplate renders a string using the provided template and data
func StringToBase64 ¶
StringToBase64 encodes a string to Base64
func StringToMD5 ¶
StringToMD5 calculates the MD5 hash of a string
func StringToSHA1 ¶
StringToSHA1 calculates the SHA1 hash of a string
func StringToSHA256 ¶
StringToSHA256 calculates the SHA256 hash of a string
func StringTruncate ¶
StringTruncate truncates a string to the specified maximum length and adds ellipsis If maxLen is less than or equal to 3, it will simply truncate without ellipsis
func UniqueElements ¶
func UniqueElements[T ElementType](elements []T) []T
UniqueElements 过滤切片中的重复元素
func ValidateToken ¶
ValidateToken 验证 JWT token 是否有效
func WriteJSONFile ¶
WriteJSONFile 将结构体数据写入JSON文件
func WriteLines ¶
WriteLines 将字符串切片写入文件,每行一个字符串
func WriteStringToFile ¶
WriteStringToFile 安全地将字符串写入文件
Types ¶
type ElementType ¶
type ElementType interface{ eleNum | string }
type HTTPClient ¶
HTTPClient 是HTTP客户端的接口定义
type HTTPRequest ¶
type HTTPRequest struct {
URL string
Method string
Headers map[string]string
QueryParams map[string]string
Body interface{}
Timeout time.Duration
Client HTTPClient
Context context.Context
}
HTTPRequest 封装HTTP请求的参数
func (*HTTPRequest) SetBody ¶
func (r *HTTPRequest) SetBody(body interface{}) *HTTPRequest
SetBody 设置请求体
func (*HTTPRequest) SetClient ¶
func (r *HTTPRequest) SetClient(client HTTPClient) *HTTPRequest
SetClient 设置HTTP客户端
func (*HTTPRequest) SetContext ¶
func (r *HTTPRequest) SetContext(ctx context.Context) *HTTPRequest
SetContext 设置上下文
func (*HTTPRequest) SetHeader ¶
func (r *HTTPRequest) SetHeader(key, value string) *HTTPRequest
SetHeader 设置单个请求头
func (*HTTPRequest) SetHeaders ¶
func (r *HTTPRequest) SetHeaders(headers map[string]string) *HTTPRequest
SetHeaders 批量设置请求头
func (*HTTPRequest) SetMethod ¶
func (r *HTTPRequest) SetMethod(method string) *HTTPRequest
SetMethod 设置HTTP方法
func (*HTTPRequest) SetQueryParam ¶
func (r *HTTPRequest) SetQueryParam(key, value string) *HTTPRequest
SetQueryParam 设置单个查询参数
func (*HTTPRequest) SetQueryParams ¶
func (r *HTTPRequest) SetQueryParams(params map[string]string) *HTTPRequest
SetQueryParams 批量设置查询参数
func (*HTTPRequest) SetTimeout ¶
func (r *HTTPRequest) SetTimeout(timeout time.Duration) *HTTPRequest
SetTimeout 设置请求超时时间
type HTTPResponse ¶
HTTPResponse 封装HTTP响应
type JWTClaims ¶
type JWTClaims struct {
UserID uint `json:"user_id"`
Username string `json:"username"`
jwt.RegisteredClaims
}
JWTClaims 自定义的 JWT Claims 结构
type JWTConfig ¶
type JWTConfig struct {
SecretKey string // JWT 密钥
ExpireTime time.Duration // 过期时间
Issuer string // 签发者
SigningMethod string // 签名方法
}
JWTConfig JWT 配置结构
type SyncMap ¶
type SyncMap struct {
// contains filtered or unexported fields
}
func NewSyncMap ¶
func NewSyncMap() *SyncMap