🎯 比 lodash 还全,比 slices 包更偏业务 — Go 业务工具集
一个 Go 通用工具库:50+ 个常用函数(切片/集合/字符串/数值)+ 业务子包
biz(重试/分页)。所有公开函数都有 godoc Example 可执行。
- 📚 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/operimport (
"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})| 类别 | 函数 |
|---|---|
| 切片 | 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 |
| 类别 | 函数 |
|---|---|
| 重试 | Retry(ctx, max, fn) 指数退避 + jitter · RetryWithResult[T] 泛型 |
| 分页 | Page{Page, PageSize} 规整 · NewPageResult[T] 构造响应 |
| 能力 | slices(官方) |
oper |
|---|---|---|
| 基础操作 | ✅ | ✅ |
| Chunk / GroupBy / Flatten | ❌ | ✅ |
| UniqBy / Drop / Take | 部分 | ✅ 全 |
| Map / Filter / Reduce | ❌ | ✅ |
嵌套取值 Get(obj, "a.b") |
❌ | ✅ |
| 集合运算(差/交/并) | ❌ | ✅ |
| 泛型重试 / 分页 | ❌ | ✅ biz |
简单切片用 slices,复杂业务来 oper。
12+ 个公开函数都有可执行 Example,pkg.go.dev 上直接看到。
go get github.com/moercat/oper@v1.1.0要求 Go 1.18+(泛型)。