Documentation
¶
Index ¶
- Constants
- Variables
- func AddDir(path string) error
- func AddFile(path string) error
- func AddFileIfNotExists(path string) error
- func AssignIfNotNil[T any](dst *T, src *T)
- func GenerateRandomString(length int) string
- func GetRealClientIP(r *http.Request) string
- func IsExpired(lastTime time.Time, duration time.Duration) bool
- func IsWithinDuration(lastTime time.Time, duration time.Duration) bool
- func JoinInt64s(nums []int64) string
- func ParamToInt64(c *gin.Context, key string) (int64, error)
- func StructToMap(obj interface{}) map[string]string
- type Cache
- type Time
- type TreeService
Constants ¶
const (
DefaultTimeFormat = time.DateTime
)
Variables ¶
ErrCacheUnavailable 表示 Redis 客户端尚未初始化
Functions ¶
func AddFileIfNotExists ¶
AddFileIfNotExists 若文件不存在则创建(包括父目录),已存在时直接返回 nil. 注意:检查与创建之间存在 TOCTOU 窗口,并发调用方应自行保证互斥.
func GenerateRandomString ¶
GenerateRandomString 生成指定长度的随机字符串
func GetRealClientIP ¶
GetRealClientIP 获取真实的客户端IP地址. 依次检查常见代理头,跳过无效/内网地址,全部无效时回退到 RemoteAddr.
func IsWithinDuration ¶
IsWithinDuration 检查时间是否在指定时间段内
func ParamToInt64 ¶
ParamToInt64 从 URL 参数中获取 int64 类型的值.
func StructToMap ¶
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 缓存接口
type Time ¶
func (*Time) UnmarshalJSON ¶
UnmarshalJSON 格式化自定义时间转换为 time.Time. 仅接受 DefaultTimeFormat 格式的 JSON 字符串,解析失败直接返回错误,不吞 panic.
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输出, 参数同上.