Documentation
¶
Index ¶
- Constants
- func AddSlashes(s string) string
- func CopyProperties(dst, src interface{}) error
- func CopyPropertiesNonBlank(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 FileHash(reader io.Reader, algorithm string) string
- func FormatTime(t time.Time, format ...string) string
- 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 ParseTimeByStr(value string) time.Time
- 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 Pager
- type PagerResult
- type Time
- type X
Constants ¶
const ( // DefaultTimeLayout 默认展示格式。 DefaultTimeLayout = "2006-01-02 15:04:05" // DateLayout 日期格式。 DateLayout = "2006-01-02" )
Time 是封装后的时间类型(内嵌 time.Time):
- JSON 序列化自动输出 "2006-01-02 15:04:05"(零值输出空串)——接口返回无需额外格式化
- JSON 反序列化容错解析(多种格式)
- GORM 兼容(实现 Valuer/Scanner,数据库字段照常存取)
- 保留 time.Time 全部方法(t.Time() 获取原始值,兼容老逻辑)
用法:
type Order struct {
PaidAt util.Time `json:"paid_at"` // 数据库存 DATETIME,接口返回 "2026-08-16 01:30:00"
}
对齐 sg-mes-api 痛点:model 时间字段无需在接口查询时额外 FormatTime。
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 之间自动转换
嵌套 struct 递归逐字段拷贝(深度上限 8,防循环)
时间互转:util.Time ↔ time.Time ↔ string(自动格式化/解析)
未导出字段、不可设置字段跳过
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 CopyPropertiesNonBlank ¶ added in v1.33.0
func CopyPropertiesNonBlank(dst, src interface{}) error
CopyPropertiesNonBlank 拷贝属性,但 src 的零值字段不覆盖 dst(空值保护)。 对标 sg-mes-api 的 SimpleCopyProperties2 行为:
- 字段名 + 类型一致才拷贝
- src 字段为零值(空串/0/false/nil)时跳过,保留 dst 原值
- 嵌套 struct 递归逐字段(空值保护生效);时间类型自动互转
与 CopyProperties(无条件覆盖)互补:更新场景用本函数,避免把空值写库。
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 FileHash ¶ added in v1.33.0
FileHash 计算流式文件/数据哈希(对齐 sg-mes FileHash)。 algorithm: "md5" 或 "sha1"(默认 sha1);返回小写 hex。
func FormatTime ¶ added in v1.33.0
FormatTime 格式化时间;零值(0001-01-01)返回空串(数据库未赋值场景友好)。 format 为空时使用 "2006-01-02 15:04:05"。
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 ParseTimeByStr ¶ added in v1.33.0
ParseTimeByStr 容错解析时间字符串;解析失败返回零值(不 panic)。 支持格式按顺序尝试:2006-01-02 15:04:05 / 2006-01-02 15:04 / 2006-01-02。
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` 标记序列化
type Pager ¶ added in v1.33.0
type Pager struct {
PageNo int // 页码(从 1 开始;<=0 时按 1)
PageSize int // 每页数量(<=0 时按 10;>100 时截断为 100)
Count int64 // 总条数(查询后设置,用于计算分页信息)
}
Pager 分页参数与计算(对齐 sg-mes 的 PagerReq/PagerRes)。
type PagerResult ¶ added in v1.33.0
type PagerResult struct {
List interface{} `json:"list"`
PageNo int `json:"page_no"`
PageSize int `json:"page_size"`
Total int64 `json:"total"`
TotalPage int `json:"total_page"`
FirstPage bool `json:"first_page"`
LastPage bool `json:"last_page"`
}
PagerResult 分页响应(webiris.OK 的 data 用)。
type Time ¶ added in v1.34.0
Time 时间类型。
func (Time) MarshalJSON ¶ added in v1.34.0
MarshalJSON 序列化为 "2006-01-02 15:04:05"(零值输出空串)。
func (*Time) UnmarshalJSON ¶ added in v1.34.0
UnmarshalJSON 容错解析。