fn

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2025 License: Apache-2.0 Imports: 31 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CalculateRealSize added in v0.0.7

func CalculateRealSize(v interface{}) int

计算真实数据总和

func CamelToSnake added in v0.1.2

func CamelToSnake(s string) string

func CheckModelValid added in v0.1.2

func CheckModelValid(model map[string]any) error

CheckModelValid 检查模型中每个键是否合法:非空且只允许英文、数字、下划线以及中文字符

func CollectToMap added in v0.1.2

func CollectToMap[T any, K comparable, V any](
	in []T,
	keyOf func(T) K,
	valOf func(T) V,
) map[K]V

func ConvertByJSON added in v0.0.7

func ConvertByJSON[A any, B any](input A) (B, error)

ConvertByJSON Convert 转换任意类型 A 到类型 B

func DeRef added in v0.1.2

func DeRef(row []interface{}) []interface{}

func DefaultTemplateRender added in v0.1.2

func DefaultTemplateRender(inputText string, sourceMap map[string]any, result *string) error

func DownloadToTempDir

func DownloadToTempDir(url string) (*os.File, error)

func FileExists

func FileExists(filePath string) bool

func Filter added in v0.0.7

func Filter[T any](endpoints []T, f func(endpoint T) bool) []T

func Find added in v0.1.2

func Find[T any](list []T, predicate func(T) bool) (T, bool)

func GenerateShortID added in v0.1.2

func GenerateShortID() (string, error)

func GenerateShortIDWithDigits added in v0.1.2

func GenerateShortIDWithDigits(digits uint8) (string, error)

func GetByPath added in v0.1.2

func GetByPath(data any, path []any) any

GetByPath 从嵌套结构中通过解析后的路径获取值

func GetSuffix added in v0.1.2

func GetSuffix(str string) string

func GetValue added in v0.1.2

func GetValue(data map[string]any, expr string) any

GetValue 从 map 中通过路径表达式获取值(支持中文、嵌套、数组)

func GzipCompress

func GzipCompress(input []byte) []byte

func GzipDecompress

func GzipDecompress(input []byte) []byte

func HashPassword added in v0.1.2

func HashPassword(password string) (string, error)

HashPassword 使用 bcrypt 对密码进行哈希处理

func InLike added in v0.1.2

func InLike(target any, list any) bool

func InSlice added in v0.1.2

func InSlice(target any, list any) bool

func InitSnowflake added in v0.1.2

func InitSnowflake(nodeID int64) (*snowflake.Node, error)

InitSnowflake 初始化 Snowflake 节点

func IsDataEmpty added in v0.1.2

func IsDataEmpty(v any) bool

func IsEmpty

func IsEmpty(s string) bool

func MapKeys added in v0.1.2

func MapKeys[M ~map[K]V, K comparable, V any](m M) []string

func MustJSON added in v0.1.2

func MustJSON(v any) datatypes.JSON

func NumericStringify added in v0.1.2

func NumericStringify(v any) string

func ParseList added in v0.1.2

func ParseList(v any) []string

func ParsePathExpr added in v0.1.2

func ParsePathExpr(expr string) []any

ParsePathExpr 将路径字符串(如 用户.好友[0].昵称)解析为 ["用户", "好友", 0, "昵称"]

func ParseTemplate added in v0.1.2

func ParseTemplate(templateText string) (map[string]TemplateNeedle, error)

ParseTemplate 从模板文本中提取所有占位符,返回键值对:键为参数名称,值为占位符文本

func ParseTemplateTest added in v0.1.2

func ParseTemplateTest()

func Ptr added in v0.1.2

func Ptr[T any](v T) *T

func RegexReplace added in v0.1.2

func RegexReplace(s, pattern, repl string) string

RegexReplace 用正则表达式将字符串 s 中所有匹配 pattern 的部分替换为 repl

func RenderTemplateStrictly added in v0.1.2

func RenderTemplateStrictly(templateText string, slots map[string]TemplateNeedle, model map[string]any, strict bool) string

RenderTemplateStrictly 根据传入的模型,将模板中的占位符替换为实际值

func RunParallel added in v0.1.2

func RunParallel[I any, O any](
	ctx context.Context,
	inputs []I,
	workers int,
	rowId func(I) string,
	function func(context.Context, I) (O, error),
) (map[string]O, []error)

RunParallel 并发执行 fn,按输入顺序返回结果。 - inputs: 要处理的输入切片 - workers: 工作者数量(<=0 视为 1) - fn: 你的任务函数,形如 func(ctx, I) (O, error)

func SaveToWAVFile

func SaveToWAVFile(filename string, data []byte) (*os.File, error)

func ScryptHash added in v0.1.2

func ScryptHash(password, salt string) (string, error)

func SendFileChunks

func SendFileChunks(file *os.File, firstSize int, chunkSize int, ch chan []byte)

func SimplifyHeader added in v0.0.7

func SimplifyHeader(header map[string][]string) map[string]string

SimplifyHeader 压缩 Header

func StreamMap added in v0.1.2

func StreamMap[T any, R any](in []T, f func(T) R) []R

func Stringify added in v0.1.2

func Stringify[A any](input A) string

func Ternary

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

Ternary 三元表达式

func TernaryFunc added in v0.1.2

func TernaryFunc[T any](cond bool, trueVal func() T, falseVal T) T

func TimingMiddlewareLogging added in v0.1.2

func TimingMiddlewareLogging(phase string, log string) func()

func ToFloat64 added in v0.1.2

func ToFloat64(v any) (float64, bool)

func UniqueList added in v0.1.2

func UniqueList(items []string) []string

func UniqueStrings added in v0.1.2

func UniqueStrings(input []string) []string

func Uuid added in v0.1.2

func Uuid() string

Types

type FlexibleFloat64 added in v0.1.2

type FlexibleFloat64 float64

func (*FlexibleFloat64) UnmarshalJSON added in v0.1.2

func (f *FlexibleFloat64) UnmarshalJSON(b []byte) error

type FlexibleInt64 added in v0.1.2

type FlexibleInt64 int64

func (*FlexibleInt64) UnmarshalJSON added in v0.1.2

func (f *FlexibleInt64) UnmarshalJSON(b []byte) error

type FlexibleInt8 added in v0.1.2

type FlexibleInt8 int8

func (*FlexibleInt8) UnmarshalJSON added in v0.1.2

func (f *FlexibleInt8) UnmarshalJSON(b []byte) error

type HandlerFunc added in v0.1.2

type HandlerFunc[T any] func(items []T)

type KeyFunc added in v0.1.2

type KeyFunc[T any] func(item T) string

type LRUCache added in v0.1.2

type LRUCache[K comparable, V any] struct {
	// contains filtered or unexported fields
}

LRUCache 是一个并发安全的泛型 LRU 缓存

func NewLRUCache added in v0.1.2

func NewLRUCache[K comparable, V any](cap int) *LRUCache[K, V]

NewLRUCache 新建一个最大容量为 cap 的 LRUCache

func (*LRUCache[K, V]) Get added in v0.1.2

func (c *LRUCache[K, V]) Get(key K) (V, bool)

Get 取值;存在时把该条目移到队首(最近使用),返回 true;否则返回 false

func (*LRUCache[K, V]) Len added in v0.1.2

func (c *LRUCache[K, V]) Len() int

Len 返回当前缓存大小

func (*LRUCache[K, V]) Put added in v0.1.2

func (c *LRUCache[K, V]) Put(key K, value V)

Put 插入或更新:如果已存在,更新值并移到队首;否则插入新条目,超出容量时驱逐最旧的

func (*LRUCache[K, V]) Trash added in v0.1.2

func (c *LRUCache[K, V]) Trash(key K)

Trash 删除

type Option added in v0.1.2

type Option[K comparable, V any] func(*LRUCache[K, V])

type PageQueryFunc added in v0.1.2

type PageQueryFunc[T any] func(ctx context.Context, pageNum, pageSize int) ([]T, error)

type Pair added in v0.1.2

type Pair[O any] struct {
	Out O
	Err error
}

type SafeHashSet added in v0.1.2

type SafeHashSet[T comparable] struct {
	// contains filtered or unexported fields
}

func NewSafeHashSet added in v0.1.2

func NewSafeHashSet[T comparable]() *SafeHashSet[T]

func ScanAllPages added in v0.1.2

func ScanAllPages[T any](
	ctx context.Context,
	pageSize int,
	query PageQueryFunc[T],
	keyFunc KeyFunc[T],
	handle HandlerFunc[T],
) (*SafeHashSet[string], error)

func (*SafeHashSet[T]) Add added in v0.1.2

func (s *SafeHashSet[T]) Add(val T)

func (*SafeHashSet[T]) Has added in v0.1.2

func (s *SafeHashSet[T]) Has(val T) bool

func (*SafeHashSet[T]) Len added in v0.1.2

func (s *SafeHashSet[T]) Len() int

func (*SafeHashSet[T]) Remove added in v0.1.2

func (s *SafeHashSet[T]) Remove(val T)

func (*SafeHashSet[T]) Values added in v0.1.2

func (s *SafeHashSet[T]) Values() []T

type TemplateNeedle added in v0.1.2

type TemplateNeedle struct {
	Template     string `json:"template"`
	DefaultValue string `json:"defaultValue"`
}

Jump to

Keyboard shortcuts

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