util

package
v0.2.5 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2026 License: Apache-2.0 Imports: 35 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Timezone = "Asia/Shanghai"
View Source
var TimezoneLocation *time.Location
View Source
var TimezoneOffset = 8 * 3600 // 东八区的时间偏移量,单位为秒

Functions

func AesDecECBString added in v0.0.20

func AesDecECBString(ciphertext []byte, key []byte) (string, error)

AES-128-ECB 解密函数 pkcs5Padding

func AesDecrypt

func AesDecrypt(decryptStr string, key []byte, iv []byte) (string, error)

aes 解密 aes-128-cbc pkcs5Padding

func AesEncECBString added in v0.0.20

func AesEncECBString(plaintext []byte, key []byte) ([]byte, error)

AES-128-ECB 加密函数 pkcs5Padding

func AesEncrypt

func AesEncrypt(encryptStr string, key []byte, iv []byte) (string, error)

aes 加密 aes-128-cbc pkcs5Padding

func Base64DecodeString added in v0.0.20

func Base64DecodeString(text, tp string) (string, error)

base64 decode

func Base64EncodeString added in v0.0.20

func Base64EncodeString(text []byte, tp string) string

base64 encode

func CheckPhoneNumber

func CheckPhoneNumber(phoneNumber string) bool

检查手机号码格式

func ConnectSftp

func ConnectSftp(user, password, host string, port int) (*sftp.Client, error)

ConnectSftp 创建 SFTP 客户端连接 !!!! 即将废弃,请参考 gitee.com/aly1a/boat-go/module/sftp 包中的 SftpClient 结构体和相关方法

func ContainUpperLowerNumberSpecial

func ContainUpperLowerNumberSpecial(str string, num int) bool

检查是否包含大小写字母以及数字和特殊字符中的指定位数

func ConvertTo added in v0.2.4

func ConvertTo[T any](value any) (T, error)

ConvertTo 安全的类型转换函数

func ConvertToWithDefault added in v0.2.4

func ConvertToWithDefault[T any](value any, defaultValue T) T

ConvertToWithDefault 带默认值的转换

func DynamicGetStructField added in v0.2.4

func DynamicGetStructField(s any, fieldPath string, capitalizeFirst bool) (any, error)

DynamicGetStructField 获取结构体字段值(支持点号分隔的路径)

func DynamicGetStructFieldByList added in v0.2.4

func DynamicGetStructFieldByList(s any, fieldNames []string, capitalizeFirst bool) (any, error)

DynamicGetStructFieldByList 按字段列表递归获取值

func DynamicUpdateStructField added in v0.2.4

func DynamicUpdateStructField(s any, fieldName string, newValue interface{}) error

DynamicUpdateStructField 动态修改结构体字段值

func EndPadByte

func EndPadByte(data []byte, pad string, size int) []byte

EndPadByte 向后补充字节到指定长度

func EndPadString

func EndPadString(text, pad string, size int) string

EndPadString 向后补充字符串到指定长度

func FirstNonZero added in v0.0.20

func FirstNonZero[T comparable](values ...T) T

FirstNonZero 返回第一个非零值

func FirstNonZeroByAny added in v0.0.20

func FirstNonZeroByAny(values ...any) any

FirstNonZeroByAny 返回第一个非空值

func FormatAndSortJSON added in v0.0.20

func FormatAndSortJSON(jsonStr string) (string, error)

FormatAndSortJSON 格式化并排序 JSON 字符串

func FormatDurationFromMilliseconds added in v0.2.0

func FormatDurationFromMilliseconds(ms int64) string

格式时间从毫秒开始

func GetClientIp

func GetClientIp(c *fasthttp.RequestCtx) string

GetClientIp 获取客户端真实 IP 地址

func GetFormatTimeByTimestamp added in v0.2.4

func GetFormatTimeByTimestamp(ts int64) string

通过时间戳获取格式化的时间字符串

func GetTimeByTimestamp added in v0.0.22

func GetTimeByTimestamp(ts int64) string

通过时间戳获取格式化的时间字符串

func GetTimestampByStr added in v0.0.22

func GetTimestampByStr(datetime string) int64

格式化的时间字符串转时间戳

func GlobDir

func GlobDir(dir string, fileList *FileListTmp) error

GlobDir 遍历目录下所有文件

func HasKeyBoardContinuousChar

func HasKeyBoardContinuousChar(pwd string, keyBoardSize int, dictionary string) bool

键盘序检查

func Includes

func Includes[S ~[]E, E comparable](strArr S, subStr E) bool

是否包含某个字符串

func IncludesAny

func IncludesAny(arr []any, s any) bool

inludes interface slices.Contains 不能比较 []any

func InterfaceToJson

func InterfaceToJson(i any) (string, error)

转换成json

func IsDigit added in v0.0.20

func IsDigit(text string) bool

判断字符串是否全为数字

func IsEmpty added in v0.0.12

func IsEmpty(v any) bool

IsEmpty 判断值是否为PHP风格的"empty"

func IsEmpty2 added in v0.0.12

func IsEmpty2(v any) bool

字符串"0"不为空

func IsValidIP added in v0.0.20

func IsValidIP(ip string) bool

检查IP合法性

func IsZero added in v0.0.20

func IsZero[T comparable](v T) bool

是否是零值

func JSFilter added in v0.0.22

func JSFilter[T any](s []T, f func(int, T) bool) []T

定义一个泛型函数 Filter,接收一个切片 arr 和一个判断函数 fn,返回过滤后的新切片

func JSMap added in v0.0.22

func JSMap[T any, R any](s []T, f func(T) R) []R

注意值的引用

func Length added in v0.0.20

func Length(strs ...string) int

Length 获取字符串的字符数(非字节数)

func Md5Encode

func Md5Encode(text string) string

md5 加密

func Nanoid

func Nanoid(l int) string

Nanoid 生成指定长度的随机字符串

func NanoidAlphabet

func NanoidAlphabet(l int, alphabets ...string) string

NanoidAlphabet 使用自定义字母表生成随机字符串

func NanoidAlphabetType

func NanoidAlphabetType(alphabetType string, l int) string

NanoidAlphabetType 使用类型化字母表生成随机字符串 alphabetType: "number,upperCase,lowerCase" 组合

func ParseTime added in v0.2.0

func ParseTime(layout, value string) (time.Time, error)

时间字符串转时间

func ParseTimeByTimestamp added in v0.2.4

func ParseTimeByTimestamp(ts int64) time.Time

时间戳转时间

func PathExists added in v0.0.20

func PathExists(path string) (bool, error)

PathExists 检查路径是否存在

func ReadLargeFileByLine added in v0.2.0

func ReadLargeFileByLine(filename string, processFunc func(int, string, ...any) bool, options ...any) error

ReadLargeFileByLine 按行读取文件

func SendSftp

func SendSftp(user, password, host string, port int, filename string, remoteFilePath string) (int, error)

SendSftp 发送单个文件到 SFTP 服务器

func SetNestedField added in v0.2.0

func SetNestedField(data interface{}, fieldName string, value string) error

SetNestedField 设置嵌套字段的值 data: 要设置字段值的数据结构,支持嵌套结构体 fieldName: 字段名,支持嵌套字段,用点号分隔(如 "field1.field2.field3") value: 要设置的值,字符串类型,会根据目标字段类型进行类型转换 返回值: 如果设置成功返回nil,如果发生错误返回error

func Sha256Encode

func Sha256Encode(context string) string

sha256加密

func ShuffleString

func ShuffleString(text string) string

ShuffleString 字符串乱序

func SortKeysRecursive added in v0.0.20

func SortKeysRecursive(v interface{}) interface{}

SortKeysRecursive 递归对 map 的 key 排序,并处理嵌套结构

func StartPadString

func StartPadString(text, pad string, size int) string

StartPadString 向前补充字符串到指定长度

func StringsConcat added in v0.2.4

func StringsConcat(strs ...string) string

StringsConcat 高效字符串拼接(预分配内存)

func StringsJoin added in v0.2.4

func StringsJoin(strs ...string) string

StringsJoin 简单字符串拼接

func SubStrStr

func SubStrStr(s, substr string) string

SubStrStr 从字符串最后一次出现的位置开始截取到末尾

func Ternary added in v0.0.20

func Ternary[T any](condition bool, trueVal, falseVal T) T

Ternary 三元运算符

func TimeToObjectId added in v0.2.0

func TimeToObjectId(t time.Time) primitive.ObjectID

time to mongodb object id

func Ucs2Decode added in v0.0.12

func Ucs2Decode(hexStr string, endianness string) (string, error)

UCS2 解码

func Ucs2Encode added in v0.0.12

func Ucs2Encode(str string, endianness string) (string, error)

UCS2 编码

func UniqueByKey added in v0.0.20

func UniqueByKey[T, E comparable](data []map[T]E, key T) []map[T]E

对[]map[string]any进行去重

func UploadFileToSftp

func UploadFileToSftp(sftpClient *sftp.Client, localFilename string, remotePath string) (int, error)

UploadFileToSftp 上传文件到 SFTP 服务器

func ValidatePhoneNumber

func ValidatePhoneNumber(tel string) bool

验证电话号码

Types

type FileListTmp

type FileListTmp struct {
	DirBase string
	Files   []string
	Childs  []*FileListTmp
}

FileListTmp 目录文件列表结构

type SendSftpResult added in v0.2.5

type SendSftpResult struct {
	File string
	Size int
	Err  error
}

SendSftpResult SFTP 发送结果

func SendSftps

func SendSftps(user, password, host string, port int, filenames []string, remoteFilePath string) ([]SendSftpResult, error)

SendSftps 批量发送文件到 SFTP 服务器

type StringBuilder added in v0.2.5

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

StringBuilder 高性能字符串拼接器

func NewStringBuilder added in v0.2.4

func NewStringBuilder(initialCapacity int) *StringBuilder

NewStringBuilder 创建拼接器,可预分配容量

func (*StringBuilder) Append added in v0.2.5

func (sb *StringBuilder) Append(strs ...string) *StringBuilder

Append 追加字符串

func (*StringBuilder) AppendRune added in v0.2.5

func (sb *StringBuilder) AppendRune(r rune) *StringBuilder

AppendRune 追加单个字符

func (*StringBuilder) Len added in v0.2.5

func (sb *StringBuilder) Len() int

Len 返回当前内容长度

func (*StringBuilder) Reset added in v0.2.5

func (sb *StringBuilder) Reset() *StringBuilder

Reset 重置拼接器以便复用

func (*StringBuilder) String added in v0.2.5

func (sb *StringBuilder) String() string

String 获取最终结果

Jump to

Keyboard shortcuts

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