util

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultTimeFormat = time.DateTime
)

Variables

View Source
var ErrCacheUnavailable = errors.New("redis client is not initialized")

ErrCacheUnavailable 表示 Redis 客户端尚未初始化

Functions

func AddDir

func AddDir(path string) error

AddDir 创建目录(含父目录).

func AddFile

func AddFile(path string) error

AddFile 创建空文件;文件已存在时不覆盖、不截断,直接返回 nil. 创建后立即关闭文件句柄,避免句柄泄漏.

func AddFileIfNotExists

func AddFileIfNotExists(path string) error

AddFileIfNotExists 若文件不存在则创建(包括父目录),已存在时直接返回 nil. 注意:检查与创建之间存在 TOCTOU 窗口,并发调用方应自行保证互斥.

func AssignIfNotNil

func AssignIfNotNil[T any](dst *T, src *T)

AssignIfNotNil 检查指针是否非nil,非nil时赋值

func GenerateRandomString

func GenerateRandomString(length int) string

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

func GetRealClientIP

func GetRealClientIP(r *http.Request) string

GetRealClientIP 获取真实的客户端IP地址. 依次检查常见代理头,跳过无效/内网地址,全部无效时回退到 RemoteAddr.

func IsExpired

func IsExpired(lastTime time.Time, duration time.Duration) bool

IsExpired 检查时间是否过期

func IsWithinDuration

func IsWithinDuration(lastTime time.Time, duration time.Duration) bool

IsWithinDuration 检查时间是否在指定时间段内

func JoinInt64s

func JoinInt64s(nums []int64) string

JoinInt64s 连接int64数组

func ParamToInt64

func ParamToInt64(c *gin.Context, key string) (int64, error)

ParamToInt64 从 URL 参数中获取 int64 类型的值.

func StructToMap

func StructToMap(obj interface{}) map[string]string

StructToMap converts a struct (or a non-nil pointer to struct) to a map. nil、非结构体返回 nil;未导出字段会被跳过,避免 reflect.Value.Interface panic.

Types

type Cache

type Cache interface {
	Get(ctx context.Context, key string) (string, error)
	Set(ctx context.Context, key string, value string, expiration time.Duration) error
	Delete(ctx context.Context, key string) error
	Exists(ctx context.Context, key string) (bool, error)
	Close()
}

Cache 缓存接口

func NewCache

func NewCache() Cache

NewCache 创建 Redis 缓存

type Time

type Time time.Time

func FromTime

func FromTime(t time.Time) Time

FromTime 标准时间转换 (辅助方法)

func Now

func Now() Time

Now 获取当前时间 (实用方法)

func (Time) MarshalJSON

func (t Time) MarshalJSON() ([]byte, error)

MarshalJSON 自定义时间格式化输出

func (*Time) Scan

func (t *Time) Scan(value interface{}) error

Scan 实现 GORM 的 Scanner 接口 将数据库返回的值转换为 util.Time 类型

func (Time) String

func (t Time) String() string

String 格式化输出

func (Time) ToTime

func (t Time) ToTime() time.Time

ToTime 转换为标准时间 (辅助方法)

func (*Time) UnmarshalJSON

func (t *Time) UnmarshalJSON(data []byte) error

UnmarshalJSON 格式化自定义时间转换为 time.Time. 仅接受 DefaultTimeFormat 格式的 JSON 字符串,解析失败直接返回错误,不吞 panic.

func (Time) Value

func (t Time) Value() (driver.Value, error)

Value 实现 GORM 的 Valuer 接口 将 util.Time 类型转换为数据库可接受的值

type TreeService

type TreeService struct {
	PrimaryKey  string
	ParentKey   string
	ExpandedKey string
	LeafKey     string
	ChildrenKey string
	Expanded    bool
	Result      []map[string]interface{}
	Level       map[interface{}]int
}

TreeService is a service for tree data.

func NewTreeService

func NewTreeService(config TreeService) *TreeService

NewTreeService returns a new TreeService. 未显式配置的 Key/Expanded 使用默认值;Result/Level 每次实例化都是独立的.

func (*TreeService) MakeTree

func (t *TreeService) MakeTree(data []map[string]interface{}) []map[string]interface{}

MakeTree is a function for make tree data.

@param data is a data for make tree.
@return a tree data.

func (*TreeService) MakeTreeForHTML

func (t *TreeService) MakeTreeForHTML(data []map[string]interface{}) []map[string]interface{}

MakeTreeForHTML 生成线性结构, 便于HTML输出, 参数同上.

Jump to

Keyboard shortcuts

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