# lo - Iterate over slices, maps, channels...
[![tag](https://img.shields.io/github/tag/samber/lo.svg)](https://github.com/samber/lo/releases)
![Go Version](https://img.shields.io/badge/Go-%3E%3D%201.18-%23007d9c)
[![GoDoc](https://godoc.org/github.com/samber/lo?status.svg)](https://pkg.go.dev/github.com/samber/lo)
![Build Status](https://github.com/samber/lo/actions/workflows/test.yml/badge.svg)
[![Go report](https://goreportcard.com/badge/github.com/samber/lo)](https://goreportcard.com/report/github.com/samber/lo)
[![Coverage](https://img.shields.io/codecov/c/github/samber/lo)](https://codecov.io/gh/samber/lo)
[![Contributors](https://img.shields.io/github/contributors/samber/lo)](https://github.com/samber/lo/graphs/contributors)
[![License](https://img.shields.io/github/license/samber/lo)](./LICENSE)
⨠**`samber/lo` is a Lodash-style Go library based on Go 1.18+ Generics.**
This project started as an experiment with the new generics implementation. It may look like [Lodash](https://github.com/lodash/lodash) in some aspects. I used to code with the fantastic ["go-funk"](https://github.com/thoas/go-funk) package, but "go-funk" uses reflection and therefore is not typesafe.
As expected, benchmarks demonstrate that generics are much faster than implementations based on the "reflect" package. Benchmarks also show similar performance gains compared to pure `for` loops. [See below](#-benchmark).
In the future, 5 to 10 helpers will overlap with those coming into the Go standard library (under package names `slices` and `maps`). I feel this library is legitimate and offers many more valuable abstractions.
**See also:**
- [samber/do](https://github.com/samber/do): A dependency injection toolkit based on Go 1.18+ Generics
- [samber/mo](https://github.com/samber/mo): Monads based on Go 1.18+ Generics (Option, Result, Either...)
**Why this name?**
I wanted a **short name**, similar to "Lodash" and no Go package currently uses this name.
![lo](img/logo-full.png)
## ð Install
```sh
go get github.com/samber/lo@v1
```
This library is v1 and follows SemVer strictly.
No breaking changes will be made to exported APIs before v2.0.0.
This library has no dependencies outside the Go standard library.
## ð¡ Usage
You can import `lo` using:
```go
import (
"github.com/samber/lo"
lop "github.com/samber/lo/parallel"
)
```
Then use one of the helpers below:
```go
names := lo.Uniq[string]([]string{"Samuel", "John", "Samuel"})
// []string{"Samuel", "John"}
```
Most of the time, the compiler will be able to infer the type so that you can call: `lo.Uniq([]string{...})`.
### Tips for lazy developers
I cannot recommend it, but in case you are too lazy for repeating `lo.` everywhere, you can import the entire library into the namespace.
```go
import (
. "github.com/samber/lo"
)
```
I take no responsibility on this junk. ð ð©
## ð¤ Spec
GoDoc: [https://godoc.org/github.com/samber/lo](https://godoc.org/github.com/samber/lo)
Supported helpers for slices:
- [Filter](#filter)
- [Map](#map)
- [FilterMap](#filtermap)
- [FlatMap](#flatmap)
- [Reduce](#reduce)
- [ReduceRight](#reduceright)
- [ForEach](#foreach)
- [Times](#times)
- [Uniq](#uniq)
- [UniqBy](#uniqby)
- [GroupBy](#groupby)
- [Chunk](#chunk)
- [PartitionBy](#partitionby)
- [Flatten](#flatten)
- [Interleave](#interleave)
- [Shuffle](#shuffle)
- [Reverse](#reverse)
- [Fill](#fill)
- [Repeat](#repeat)
- [RepeatBy](#repeatby)
- [KeyBy](#keyby)
- [Associate / SliceToMap](#associate-alias-slicetomap)
- [Drop](#drop)
- [DropRight](#dropright)
- [DropWhile](#dropwhile)
- [DropRightWhile](#droprightwhile)
- [Reject](#reject)
- [Count](#count)
- [CountBy](#countby)
- [CountValues](#countvalues)
- [CountValuesBy](#countvaluesby)
- [Subset](#subset)
- [Slice](#slice)
- [Replace](#replace)
- [ReplaceAll](#replaceall)
- [Compact](#compact)
- [IsSorted](#issorted)
- [IsSortedByKey](#issortedbykey)
Supported helpers for maps:
- [Keys](#keys)
- [ValueOr](#valueor)
- [Values](#values)
- [PickBy](#pickby)
- [PickByKeys](#pickbykeys)
- [PickByValues](#pickbyvalues)
- [OmitBy](#omitby)
- [OmitByKeys](#omitbykeys)
- [OmitByValues](#omitbyvalues)
- [Entries / ToPairs](#entries-alias-topairs)
- [FromEntries / FromPairs](#fromentries-alias-frompairs)
- [Invert](#invert)
- [Assign (merge of maps)](#assign)
- [MapKeys](#mapkeys)
- [MapValues](#mapvalues)
- [MapEntries](#mapentries)
- [MapToSlice](#maptoslice)
Supported math helpers:
- [Range / RangeFrom / RangeWithSteps](#range--rangefrom--rangewithsteps)
- [Clamp](#clamp)
- [Sum](#sum)
- [SumBy](#sumby)
Supported helpers for strings:
- [RandomString](#randomstring)
- [Substring](#substring)
- [ChunkString](#chunkstring)
- [RuneLength](#runelength)
Supported helpers for tuples:
- [T2 -> T9](#t2---t9)
- [Unpack2 -> Unpack9](#unpack2---unpack9)
- [Zip2 -> Zip9](#zip2---zip9)
- [Unzip2 -> Unzip9](#unzip2---unzip9)
Supported helpers for channels:
- [ChannelDispatcher](#channeldispatcher)
- [SliceToChannel](#slicetochannel)
- [Generator](#generator)
- [Buffer](#buffer)
- [BufferWithTimeout](#bufferwithtimeout)
- [FanIn](#fanin)
- [FanOut](#fanout)
Supported intersection helpers:
- [Contains](#contains)
- [ContainsBy](#containsby)
- [Every](#every)
- [EveryBy](#everyby)
- [Some](#some)
- [SomeBy](#someby)
- [None](#none)
- [NoneBy](#noneby)
- [Intersect](#intersect)
- [Difference](#difference)
- [Union](#union)
- [Without](#without)
- [WithoutEmpty](#withoutempty)
Supported search helpers:
- [IndexOf](#indexof)
- [LastIndexOf](#lastindexof)
- [Find](#find)
- [FindIndexOf](#findindexof)
- [FindLastIndexOf](#findlastindexof)
- [FindOrElse](#findorelse)
- [FindKey](#findkey)
- [FindKeyBy](#findkeyby)
- [FindUniques](#finduniques)
- [FindUniquesBy](#finduniquesby)
- [FindDuplicates](#findduplicates)
- [FindDuplicatesBy](#findduplicatesby)
- [Min](#min)
- [MinBy](#minby)
- [Max](#max)
- [MaxBy](#maxby)
- [Last](#last)
- [Nth](#nth)
- [Sample](#sample)
- [Samples](#samples)
Conditional helpers:
- [Ternary](#ternary)
- [TernaryF](#ternaryf)
- [If / ElseIf / Else](#if--elseif--else)
- [Switch / Case / Default](#switch--case--default)
Type manipulation helpers:
- [IsNil](#IsNil)
- [ToPtr](#toptr)
- [EmptyableToPtr](#emptyabletoptr)
- [FromPtr](#fromptr)
- [FromPtrOr](#fromptror)
- [ToSlicePtr](#tosliceptr)
- [ToAnySlice](#toanyslice)
- [FromAnySlice](#fromanyslice)
- [Empty](#empty)
- [IsEmpty](#isempty)
- [IsNotEmpty](#isnotempty)
- [Coalesce](#coalesce)
Function helpers:
- [Partial](#partial)
- [Partial2 -> Partial5](#partial2---partial5)
Concurrency helpers:
- [Attempt](#attempt)
- [AttemptWhile](#attemptwhile)
- [AttemptWithDelay](#attemptwithdelay)
- [AttemptWhileWithDelay](#attemptwhilewithdelay)
- [Debounce](#debounce)
- [DebounceBy](#debounceby)
- [Synchronize](#synchronize)
- [Async](#async)
- [Transaction](#transaction)
Error handling:
- [Validate](#validate)
- [Must](#must)
- [Try](#try)
- [Try1 -> Try6](#try0-6)
- [TryOr](#tryor)
- [TryOr1 -> TryOr6](#tryor0-6)
- [TryCatch](#trycatch)
- [TryWithErrorValue](#trywitherrorvalue)
- [TryCatchWithErrorValue](#trycatchwitherrorvalue)
- [ErrorsAs](#errorsas)
Constraints:
- Clonable
### Filter
Iterates over a collection and returns an array of all the elements the predicate function returns `true` for.
```go
even := lo.Filter([]int{1, 2, 3, 4}, func(x int, index int) bool {
return x%2 == 0
})
// []int{2, 4}
```
[[play](https://go.dev/play/p/Apjg3WeSi7K)]
### Map
Manipulates a slice of one type and transforms it into a slice of another type:
```go
import "github.com/samber/lo"
lo.Map([]int64{1, 2, 3, 4}, func(x int64, index int) string {
return strconv.FormatInt(x, 10)
})
// []string{"1", "2", "3", "4"}
```
[[play](https://go.dev/play/p/OkPcYAhBo0D)]
Parallel processing: like `lo.Map()`, but the mapper function is called in a goroutine. Results are returned in the same order.
```go
import lop "github.com/samber/lo/parallel"
lop.Map([]int64{1, 2, 3, 4}, func(x int64, _ i
没有合适的资源?快使用搜索试试~ 我知道了~
lo - 迭代切片、地图、通道Go 1.18+ 泛型的 Lodash 风格的 Go 库
共30个文件
go:17个
yml:5个
md:2个
0 下载量 182 浏览量
2024-05-16
16:59:19
上传
评论
收藏 47KB ZIP 举报
温馨提示
samber/lo 是一个基于 Go 1.18+ 泛型的 Lodash 风格的 Go 库。 该项目最初是作为新泛型实施的实验而开始的。它在某些方面可能看起来像 Lodash。我曾经使用出色的“go-funk”包进行编码,但是“go-funk”使用反射,因此不是类型安全的。 正如预期的那样,基准测试表明泛型比基于“reflect”包的实现快得多。与纯 for 循环相比,基准测试也显示出类似的性能提升。见下文。 将来,5 到 10 个助手将与 Go 标准库中的助手重叠(在包名称 slices 和 maps 下)。我觉得这个库是合法的,并且提供了许多更有价值的抽象。
资源推荐
资源详情
资源评论
收起资源包目录
lo-1.39.0.zip (30个子文件)
map.go 5KB
slice.go 16KB
func.go 2KB
string.go 2KB
go.mod 166B
.travis.yml 97B
.github
workflows
test.yml 576B
release.yml 1KB
lint.yml 386B
FUNDING.yml 17B
type_manipulation.go 2KB
go.sum 207B
constraints.go 125B
Makefile 1KB
LICENSE 1KB
concurrency.go 2KB
channel.go 8KB
retry.go 7KB
find.go 9KB
CHANGELOG.md 6KB
tuples.go 16KB
types.go 2KB
errors.go 9KB
Dockerfile 95B
.gitignore 757B
math.go 2KB
condition.go 3KB
intersect.go 4KB
README.md 63KB
parallel
slice.go 3KB
共 30 条
- 1
资源评论
小云同志你好
- 粉丝: 1065
- 资源: 1058
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功