Documentation
¶
Index ¶
- Constants
- func AddSlashes(s string) string
- func CopyProperties(dst, src interface{}) error
- func CreateFile(filename string) (*os.File, error)
- func Date(timestamp int64, format ...string) string
- func DeepCopy(src interface{}) (interface{}, error)
- func FieldValue(obj interface{}, fieldName string) (interface{}, error)
- func IP2Long(ip string) uint32
- func IsNilFixed(i interface{}) bool
- func LoadCertFromPfxFile(pfxFile, password string) (tls.Certificate, error)
- func Long2IP(ip uint32) string
- func MD5(s string) string
- func MD5Bytes(data []byte) string
- func MarshalNoEscapeHTML(v any) ([]byte, error)
- func OpenFile(filename string) (*os.File, error)
- func QuoteMeta(s string) string
- func RandomString(n int) (string, error)
- func RandomStringMust(n int) string
- func SHA1(s string) string
- func SHA256(s string) string
- func SetFieldValue(obj interface{}, fieldName string, value interface{}) error
- func SetTimezone(loc *time.Location)
- func SliceRand[T any](a []T, n int) []T
- func SliceUniq[T ~int | ~int64 | ~float64 | ~string](a []T) []T
- func StrToTime(datetime string, format ...string) int64
- func StripSlashes(s string) string
- func UUID() (string, error)
- func UUIDOrEmpty() string
- func VersionCompare(rangeVer, curVer string) (bool, error)
- func WeekAround(timestamp int64, format ...string) (monday, sunday string)
- type CDATA
- type X
Constants ¶
const ( // OK defines for string `OK` OK = "OK" // Default defines for `default` name Default = "default" )
Variables ¶
This section is empty.
Functions ¶
func CopyProperties ¶ added in v1.12.0
func CopyProperties(dst, src interface{}) error
CopyProperties 将 src 的导出字段按「字段名」拷贝到 dst(对标 Java BeanUtils.copyProperties)。 dst 必须为指向结构体的指针;src 为结构体或结构体指针。 规则:
字段名完全匹配(区分大小写);src 有而 dst 没有的字段忽略
类型相同直接赋值;基本数值类型(int 族/float 族)与 string 之间自动转换
dst 的零值字段会被 src 覆盖(与 BeanUtils 一致);dst 已有非零值也会被覆盖
未导出字段、不可设置字段跳过
type UserDTO struct { Name string; Age int } type UserEntity struct { Name string; Age int64; Extra string } var dto UserDTO util.CopyProperties(&dto, userEntity) // Name 直接拷贝,Age int64→int 自动转换
func CreateFile ¶
CreateFile 创建或清空指定的文件 文件已存在,则清空;文件或目录不存在,则以0775权限创建
func DeepCopy ¶ added in v1.12.0
func DeepCopy(src interface{}) (interface{}, error)
DeepCopy 深拷贝任意值(对标 Java 序列化深拷贝): 支持基本类型、string、struct、map、slice、array、指针、interface 的组合嵌套。 结构体中的未导出字段无法复制(反射限制),导出字段递归复制。
func FieldValue ¶ added in v1.12.0
FieldValue 按字段名读取结构体属性(支持嵌套字段名如 "User.Name")。
func IsNilFixed ¶
func IsNilFixed(i interface{}) bool
IsNilFixed 判断当前值是否为nil,i 必须为: Ptr、Map、Array、Chan、Slice
func LoadCertFromPfxFile ¶
func LoadCertFromPfxFile(pfxFile, password string) (tls.Certificate, error)
LoadCertFromPfxFile 通过pfx(p12)文件生成TLS证书 注意:证书需采用「TripleDES-SHA1」加密方式
func MD5 ¶ added in v1.12.0
MD5 计算字符串的 MD5 摘要(32 位小写 hex)。 注意:MD5 仅用于非安全场景(签名校验、去重、缓存键),密码存储请使用 bcrypt/argon2。
func MarshalNoEscapeHTML ¶
MarshalNoEscapeHTML 不带HTML转义的JSON序列化
func RandomString ¶ added in v1.12.0
RandomString 生成指定长度的随机字母数字串(0-9a-zA-Z)。 随机源失败时返回错误(系统级故障,不建议静默降级)。
func RandomStringMust ¶ added in v1.12.0
RandomStringMust 生成随机串;失败时 panic(仅限启动期/非关键路径使用)。
func SetFieldValue ¶ added in v1.12.0
SetFieldValue 按字段名设置结构体属性(支持嵌套字段名)。 value 为 nil 时清空该字段(设为类型零值)。
func UUIDOrEmpty ¶ added in v1.12.0
func UUIDOrEmpty() string
UUIDOrEmpty 生成 UUID;失败时返回空串(调用方不关心失败场景时使用)。
func VersionCompare ¶
VersionCompare 语义化的版本比较,支持:>, >=, =, !=, <, <=, | (or), & (and). 参数 `rangeVer` 示例:1.0.0, =1.0.0, >2.0.0, >=1.0.0&<2.0.0, <2.0.0|>3.0.0, !=4.0.4
func WeekAround ¶
WeekAround 返回给定时间戳所在的周的「周一」和「周日」;默认格式:2006-01-02 15:04:05
Types ¶
type CDATA ¶
type CDATA string
CDATA XML `CDATA` 标记
func (CDATA) MarshalXML ¶
MarshalXML XML 带 `CDATA` 标记序列化