Skip to content

Repository files navigation

oper

🎯 比 lodash 还全,比 slices 包更偏业务 — Go 业务工具集

一个 Go 通用工具库:50+ 个常用函数(切片/集合/字符串/数值)+ 业务子包 biz(重试/分页)。所有公开函数都有 godoc Example 可执行。

Go Version Status License CI Go Reference Functions

✨ 为什么是 oper

  • 📚 API 齐全 — 50+ 函数覆盖切片/集合/字符串/数值/对象
  • 📖 可运行示例 — 每个公开函数都有 godoc Example
  • 🎯 业务差异化 — 不与官方 slices 包重叠:biz.Retry / biz.Page / oper.Chunk / oper.GroupBy / oper.Get
  • 🔄 泛型支持 — Go 1.18+,类型安全
  • 📦 零依赖oper/biz 子包零外部依赖

🚀 快速开始

go get github.com/moercat/oper
import (
    "github.com/moercat/oper"
    "github.com/moercat/oper/biz"
)

nums := []int{1, 2, 3, 4, 2}
oper.In(nums, 2)                                    // true
oper.Filter(nums, func(n int) bool { return n%2 == 0 })  // [2 4 2]
oper.Chunk(nums, 2)                                 // [[1 2] [3 4] [2]]
oper.GroupBy([]string{"a", "b", "a"}, func(s string) string { return s })
// map[a:[a a] b:[b]]

err := biz.Retry(ctx, 3, func() error { return callAPI() })
res := biz.NewPageResult(items, total, biz.Page{Page: 1, PageSize: 20})

📚 API 一览(50+ 函数)

oper 主包

类别 函数
切片 In, Index, Pop, Remove, Append, Insert, Count, Equal, Extend, Reverse, Take, Drop
Python 风格 Map, Filter, Any, All, Reduce
JS 风格 Find, FindIndex, Some, Every
Lodash 风格 Chunk, Flatten, Uniq, UniqBy, GroupBy, Compact, Initial, Tail, TakeRight, DropRight
集合 Set, Add, Union, Update, Difference, Intersection, UnionSlice, SymmetricDifference
数值 Opera, Between, Range, Sum, Avg, Min, Max, Clamp, LinearMap
字符串 Capitalize, CamelCase, SnakeCase, KebabCase, PascalCase
对象 Get (嵌套值), Pick, Omit, SliceToMap, FindLast, FindLastIndex

oper/biz 子包

类别 函数
重试 Retry(ctx, max, fn) 指数退避 + jitter · RetryWithResult[T] 泛型
分页 Page{Page, PageSize} 规整 · NewPageResult[T] 构造响应

🆚 oper vs 官方 slices 包

能力 slices(官方) oper
基础操作
Chunk / GroupBy / Flatten
UniqBy / Drop / Take 部分 ✅ 全
Map / Filter / Reduce
嵌套取值 Get(obj, "a.b")
集合运算(差/交/并)
泛型重试 / 分页 biz

简单切片用 slices,复杂业务来 oper

📖 godoc Examples

12+ 个公开函数都有可执行 Example,pkg.go.dev 上直接看到。

📦 安装与版本

go get github.com/moercat/oper@v1.1.0

要求 Go 1.18+(泛型)。

About

最符合工作需要的操作包、操作工具、操作库

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages