✨ 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 in some aspects. I used to code with the fantastic "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.
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:
Why this name?
I wanted a short name, similar to "Lodash" and no Go package uses this name.

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.
You can import lo using:
import ( "github.com/samber/lo" lop "github.com/samber/lo/parallel" )
Then use one of the helpers below:
names := lo.Uniq([]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{...}).
I cannot recommend it, but in case you are too lazy for repeating lo. everywhere, you can import the entire library into the namespace.
import ( . "github.com/samber/lo" )
I take no responsibility on this junk. 😁 💩
GoDoc: https://godoc.org/github.com/samber/lo
Supported helpers for slices:
Supported helpers for maps:
Supported math helpers:
Supported helpers for strings:
Supported helpers for tuples:
Supported helpers for time and duration:
Supported helpers for channels:
Supported intersection helpers:
Supported search helpers:
Conditional helpers:
Type manipulation helpers:
Function helpers:
Concurrency helpers:
Error handling:
Constraints:
Iterates over a collection and returns an array of all the elements the predicate function returns true for.
even := lo.Filter([]int{1, 2, 3, 4}, func(x int, index int) bool { return x%2 == 0 }) // []int{2, 4}
[play]
Manipulates a slice of one type and transforms it into a slice of another type:
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]
Parallel processing: like lo.Map(), but the mapper function is called in a goroutine. Results are returned in the same order.
import lop "github.com/samber/lo/parallel" lop.Map([]int64{1, 2, 3, 4}, func(x int64, _ int) string { return strconv.FormatInt(x, 10) }) // []string{"1", "2", "3", "4"}
Returns a slice which obtained after both filtering and mapping using the given callback function.
The callback function should return two values: the result of the mapping operation and whether the result element should be included or not.
matching := lo.FilterMap([]string{"cpu", "gpu", "mouse", "keyboard"}, func(x string, _ int) (string, bool) { if strings.HasSuffix(x, "pu") { return "xpu", true } return "", false }) // []string{"xpu", "xpu"}
[play]
Manipulates a slice and transforms and flattens it to a slice of another type. The transform function can either return a slice or a nil, and in the nil case no value is added to the final slice.
lo.FlatMap([]int64{0, 1, 2}, func(x int64, _ int) []string { return []string{ strconv.FormatInt(x, 10), strconv.FormatInt(x, 10), } }) // []string{"0", "0", "1", "1", "2", "2"}
[play]
Reduces a collection to a single value. The value is calculated by accumulating the result of running each element in the collection through an accumulator function. Each successive invocation is supplied with the return value returned by the previous call.
sum := lo.Reduce([]int{1, 2, 3, 4}, func(agg int, item int, _ int) int { return agg + item }, 0) // 10
[play]
Like lo.Reduce except that it iterates over elements of collection from right to left.
result := lo.ReduceRight([][]int{{0, 1}, {2, 3}, {4, 5}}, func(agg []int, item []int, _ int) []int { return append(agg, item...) }, []int{}) // []int{4, 5, 2, 3, 0, 1}
[play]
Iterates over elements of a collection and invokes the function over each element.
import "github.com/samber/lo" lo.ForEach([]string{"hello", "world"}, func(x string, _ int) { println(x) }) // prints "hello\nworld\n"
[play]
Parallel processing: like lo.ForEach(), but the callback is called as a goroutine.
import lop "github.com/samber/lo/parallel" lop.ForEach([]string{"hello", "world"}, func(x string, _ int) { println(x) }) // prints "hello\nworld\n" or "world\nhello\n"
Iterates over collection elements and invokes iteratee for each element collection return value decide to continue or break, like do while().
list := []int64{1, 2, -42, 4} lo.ForEachWhile(list, func(x int64, _ int) bool { if x < 0 { return false } fmt.Println(x) return true }) // 1 // 2
[play]
Times invokes the iteratee n times, returning an array of the results of each invocation. The iteratee is invoked with index as argument.
import "github.com/samber/lo" lo.Times(3, func(i int) string { return strconv.FormatInt(int64(i), 10) }) // []string{"0", "1", "2"}
[play]
Parallel processing: like lo.Times(), but callback is called in goroutine.
import lop "github.com/samber/lo/parallel" lop.Times(3, func(i int) string { return strconv.FormatInt(int64(i), 10) }) // []string{"0", "1", "2"}
Returns a duplicate-free version of an array, in which only the first occurrence of each element is kept. The order of result values is determined by the order they occur in the array.
uniqValues := lo.Uniq([]int{1, 2, 2, 1}) // []int{1,


AI辅助编程,代码自动修复
Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。


AI一键生成PPT,就用博思AIPPT!
博思AIPPT,新一代的AI生成PPT平台,支持智能生成PPT、AI美化PPT、文本&链接生成PPT、导入Word/PDF/Markdown文档生成PPT等,内置海量精美PPT模板,涵盖商务、教育、科技等不同风格,同时针对每个页面提供多种版式, 一键自适应切换,完美适配各种办公场景。


AI赋能电商视觉革 命,一站式智能商拍平台
潮际好麦深耕服装行业,是国内AI试衣效果最好的软件。使用先进AIGC能力为电商卖家批量提供优质的、低成本的商拍图。合作品牌有Shein、Lazada、安踏、百丽等65个国内外头部品牌,以及国内10万+淘宝、天猫、京东等主流平台的品牌商家,为卖家节省将近85%的出图成本,提升约3倍出图效率,让品牌能够快速上架。


企业专属的AI法律顾问
iTerms是法大大集团旗下法律子品牌,基于最先进的大语言模型(LLM)、专业的法律知识库和强大的智能体架构,帮助企业扫清合规障碍,筑牢风控防线,成为您企业专属的AI法律顾问。


稳定高效的流量提升解决方案,助力品牌曝光
稳定高效的流量提升解决方案,助力品牌曝光

