- Notifications
You must be signed in to change notification settings - Fork854
💥 A Lodash-style Go library based on Go 1.18+ Generics (map, filter, contains, find...)
License
samber/lo
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
✨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 likeLodash 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 purefor
loops.See below.
In the future, 5 to 10 helpers will overlap with those coming into the Go standard library (under package namesslices
andmaps
). I feel this library is legitimate and offers many more valuable abstractions.
See also:
- samber/do: A dependency injection toolkit based on Go 1.18+ Generics
- samber/mo: Monads based on Go 1.18+ Generics (Option, Result, Either...)
Why this name?
I wanted ashort 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 importlo
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 repeatinglo.
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:
- Filter
- Map
- UniqMap
- FilterMap
- FlatMap
- Reduce
- ReduceRight
- ForEach
- ForEachWhile
- Times
- Uniq
- UniqBy
- GroupBy
- GroupByMap
- Chunk
- PartitionBy
- Flatten
- Interleave
- Shuffle
- Reverse
- Fill
- Repeat
- RepeatBy
- KeyBy
- SliceToMap / Associate
- FilterSliceToMap
- Keyify
- Drop
- DropRight
- DropWhile
- DropRightWhile
- DropByIndex
- Reject
- RejectMap
- FilterReject
- Count
- CountBy
- CountValues
- CountValuesBy
- Subset
- Slice
- Replace
- ReplaceAll
- Compact
- IsSorted
- IsSortedByKey
- Splice
Supported helpers for maps:
- Keys
- UniqKeys
- HasKey
- ValueOr
- Values
- UniqValues
- PickBy
- PickByKeys
- PickByValues
- OmitBy
- OmitByKeys
- OmitByValues
- Entries / ToPairs
- FromEntries / FromPairs
- Invert
- Assign (merge of maps)
- MapKeys
- MapValues
- MapEntries
- MapToSlice
Supported math helpers:
Supported helpers for strings:
- RandomString
- Substring
- ChunkString
- RuneLength
- PascalCase
- CamelCase
- KebabCase
- SnakeCase
- Words
- Capitalize
- Ellipsis
Supported helpers for tuples:
- T2 -> T9
- Unpack2 -> Unpack9
- Zip2 -> Zip9
- ZipBy2 -> ZipBy9
- Unzip2 -> Unzip9
- UnzipBy2 -> UnzipBy9
- CrossJoin2 -> CrossJoin2
- CrossJoinBy2 -> CrossJoinBy2
Supported helpers for time and duration:
Supported helpers for channels:
Supported intersection helpers:
- Contains
- ContainsBy
- Every
- EveryBy
- Some
- SomeBy
- None
- NoneBy
- Intersect
- Difference
- Union
- Without
- WithoutBy
- WithoutEmpty
- WithoutNth
Supported search helpers:
- IndexOf
- LastIndexOf
- Find
- FindIndexOf
- FindLastIndexOf
- FindOrElse
- FindKey
- FindKeyBy
- FindUniques
- FindUniquesBy
- FindDuplicates
- FindDuplicatesBy
- Min
- MinIndex
- MinBy
- MinIndexBy
- Earliest
- EarliestBy
- Max
- MaxIndex
- MaxBy
- MaxIndexBy
- Latest
- LatestBy
- First
- FirstOrEmpty
- FirstOr
- Last
- LastOrEmpty
- LastOr
- Nth
- NthOr
- NthOrEmpty
- Sample
- SampleBy
- Samples
- SamplesBy
Conditional helpers:
Type manipulation helpers:
- IsNil
- IsNotNil
- ToPtr
- Nil
- EmptyableToPtr
- FromPtr
- FromPtrOr
- ToSlicePtr
- FromSlicePtr
- FromSlicePtrOr
- ToAnySlice
- FromAnySlice
- Empty
- IsEmpty
- IsNotEmpty
- Coalesce
- CoalesceOrEmpty
- CoalesceSlice
- CoalesceSliceOrEmpty
- CoalesceMap
- CoalesceMapOrEmpty
Function helpers:
Concurrency helpers:
- Attempt
- AttemptWhile
- AttemptWithDelay
- AttemptWhileWithDelay
- Debounce
- DebounceBy
- Throttle
- ThrottleWithCount
- ThrottleBy
- ThrottleByWithCount
- Synchronize
- Async
- Transaction
- WaitFor
- WaitForWithContext
Error handling:
- Validate
- Must
- Try
- Try1 -> Try6
- TryOr
- TryOr1 -> TryOr6
- TryCatch
- TryWithErrorValue
- TryCatchWithErrorValue
- ErrorsAs
Constraints:
- Clonable
Iterates over a collection and returns an array of all the elements the predicate function returnstrue
for.
even:=lo.Filter([]int{1,2,3,4},func(xint,indexint)bool {returnx%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(xint64,indexint)string {returnstrconv.FormatInt(x,10)})// []string{"1", "2", "3", "4"}
[play]
Parallel processing: likelo.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(xint64,_int)string {returnstrconv.FormatInt(x,10)})// []string{"1", "2", "3", "4"}
Manipulates a slice and transforms it to a slice of another type with unique values.
typeUserstruct {NamestringAgeint}users:= []User{{Name:"Alex",Age:10}, {Name:"Alex",Age:12}, {Name:"Bob",Age:11}, {Name:"Alice",Age:20}}names:=lo.UniqMap(users,func(uUser,indexint)string {returnu.Name})// []string{"Alex", "Bob", "Alice"}
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(xstring,_int) (string,bool) {ifstrings.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 anil
, and in thenil
case no value is added to the final slice.
lo.FlatMap([]int64{0,1,2},func(xint64,_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(aggint,itemint,_int)int {returnagg+item},0)// 10
[play]
Likelo.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 {returnappend(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(xstring,_int) {println(x)})// prints "hello\nworld\n"
[play]
Parallel processing: likelo.ForEach()
, but the callback is called as a goroutine.
import lop"github.com/samber/lo/parallel"lop.ForEach([]string{"hello","world"},func(xstring,_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(xint64,_int)bool {ifx<0 {returnfalse}fmt.Println(x)returntrue})// 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(iint)string {returnstrconv.FormatInt(int64(i),10)})// []string{"0", "1", "2"}
[play]
Parallel processing: likelo.Times()
, but callback is called in goroutine.
import lop"github.com/samber/lo/parallel"lop.Times(3,func(iint)string {returnstrconv.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, 2}
[play]
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. It acceptsiteratee
which is invoked for each element in array to generate the criterion by which uniqueness is computed.
uniqValues:=lo.UniqBy([]int{0,1,2,3,4,5},func(iint)int {returni%3})// []int{0, 1, 2}
[play]
Returns an object composed of keys generated from the results of running each element of collection through iteratee.
import lo"github.com/samber/lo"groups:=lo.GroupBy([]int{0,1,2,3,4,5},func(iint)int {returni%3})// map[int][]int{0: []int{0, 3}, 1: []int{1, 4}, 2: []int{2, 5}}
[play]
Parallel processing: likelo.GroupBy()
, but callback is called in goroutine.
import lop"github.com/samber/lo/parallel"lop.GroupBy([]int{0,1,2,3,4,5},func(iint)int {returni%3})// map[int][]int{0: []int{0, 3}, 1: []int{1, 4}, 2: []int{2, 5}}
Returns an object composed of keys generated from the results of running each element of collection through iteratee.
import lo"github.com/samber/lo"groups:=lo.GroupByMap([]int{0,1,2,3,4,5},func(iint) (int,int) {returni%3,i*2})// map[int][]int{0: []int{0, 6}, 1: []int{2, 8}, 2: []int{4, 10}}
Returns an array of elements split into groups the length of size. If array can't be split evenly, the final chunk will be the remaining elements.
lo.Chunk([]int{0,1,2,3,4,5},2)// [][]int{{0, 1}, {2, 3}, {4, 5}}lo.Chunk([]int{0,1,2,3,4,5,6},2)// [][]int{{0, 1}, {2, 3}, {4, 5}, {6}}lo.Chunk([]int{},2)// [][]int{}lo.Chunk([]int{0},2)// [][]int{{0}}
[play]
Returns an array of elements split into groups. The order of grouped values is determined by the order they occur in collection. The grouping is generated from the results of running each element of collection through iteratee.
import lo"github.com/samber/lo"partitions:=lo.PartitionBy([]int{-2,-1,0,1,2,3,4,5},func(xint)string {ifx<0 {return"negative" }elseifx%2==0 {return"even" }return"odd"})// [][]int{{-2, -1}, {0, 2, 4}, {1, 3, 5}}
[play]
Parallel processing: likelo.PartitionBy()
, but callback is called in goroutine. Results are returned in the same order.
import lop"github.com/samber/lo/parallel"partitions:=lop.PartitionBy([]int{-2,-1,0,1,2,3,4,5},func(xint)string {ifx<0 {return"negative" }elseifx%2==0 {return"even" }return"odd"})// [][]int{{-2, -1}, {0, 2, 4}, {1, 3, 5}}
Returns an array a single level deep.
flat:=lo.Flatten([][]int{{0,1}, {2,3,4,5}})// []int{0, 1, 2, 3, 4, 5}
[play]
Round-robin alternating input slices and sequentially appending value at index into result.
interleaved:=lo.Interleave([]int{1,4,7}, []int{2,5,8}, []int{3,6,9})// []int{1, 2, 3, 4, 5, 6, 7, 8, 9}interleaved:=lo.Interleave([]int{1}, []int{2,5,8}, []int{3,6}, []int{4,7,9,10})// []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
[play]
Returns an array of shuffled values. Uses the Fisher-Yates shuffle algorithm.
import lom"github.com/samber/lo/mutable"list:= []int{0,1,2,3,4,5}lom.Shuffle(list)list// []int{1, 4, 0, 3, 5, 2}
[play]
Reverses array so that the first element becomes the last, the second element becomes the second to last, and so on.
import lom"github.com/samber/lo/mutable"list:= []int{0,1,2,3,4,5}lom.Reverse(list)list// []int{5, 4, 3, 2, 1, 0}
[play]
Fills elements of array withinitial
value.
typefoostruct {barstring}func (ffoo)Clone()foo {returnfoo{f.bar}}initializedSlice:=lo.Fill([]foo{foo{"a"},foo{"a"}},foo{"b"})// []foo{foo{"b"}, foo{"b"}}
[play]
Builds a slice with N copies of initial value.
typefoostruct {barstring}func (ffoo)Clone()foo {returnfoo{f.bar}}slice:=lo.Repeat(2,foo{"a"})// []foo{foo{"a"}, foo{"a"}}
[play]
Builds a slice with values returned by N calls of callback.
slice:=lo.RepeatBy(0,func (iint)string {returnstrconv.FormatInt(int64(math.Pow(float64(i),2)),10)})// []string{}slice:=lo.RepeatBy(5,func(iint)string {returnstrconv.FormatInt(int64(math.Pow(float64(i),2)),10)})// []string{"0", "1", "4", "9", "16"}
[play]
Transforms a slice or an array of structs to a map based on a pivot callback.
m:=lo.KeyBy([]string{"a","aa","aaa"},func(strstring)int {returnlen(str)})// map[int]string{1: "a", 2: "aa", 3: "aaa"}typeCharacterstruct {dirstringcodeint}characters:= []Character{ {dir:"left",code:97}, {dir:"right",code:100},}result:=lo.KeyBy(characters,func(charCharacter)string {returnstring(rune(char.code))})//map[a:{dir:left code:97} d:{dir:right code:100}]
[play]
Returns a map containing key-value pairs provided by transform function applied to elements of the given slice.If any of two pairs would have the same key the last one gets added to the map.
The order of keys in returned map is not specified and is not guaranteed to be the same from the original array.
in:= []*foo{{baz:"apple",bar:1}, {baz:"banana",bar:2}}aMap:=lo.SliceToMap(in,func (f*foo) (string,int) {returnf.baz,f.bar})// map[string][int]{ "apple":1, "banana":2 }
[play]
Returns a map containing key-value pairs provided by transform function applied to elements of the given slice.
If any of two pairs would have the same key the last one gets added to the map.
The order of keys in returned map is not specified and is not guaranteed to be the same from the original array.
The third return value of the transform function is a boolean that indicates whether the key-value pair should be included in the map.
list:= []string{"a","aa","aaa"}result:=lo.FilterSliceToMap(list,func(strstring) (string,int,bool) {returnstr,len(str),len(str)>1})// map[string][int]{"aa":2 "aaa":3}
Returns a map with each unique element of the slice as a key.
set:=lo.Keyify([]int{1,1,2,3,4})// map[int]struct{}{1:{}, 2:{}, 3:{}, 4:{}}
Drops n elements from the beginning of a slice or array.
l:=lo.Drop([]int{0,1,2,3,4,5},2)// []int{2, 3, 4, 5}
[play]
Drops n elements from the end of a slice or array.
l:=lo.DropRight([]int{0,1,2,3,4,5},2)// []int{0, 1, 2, 3}
[play]
Drop elements from the beginning of a slice or array while the predicate returns true.
l:=lo.DropWhile([]string{"a","aa","aaa","aa","aa"},func(valstring)bool {returnlen(val)<=2})// []string{"aaa", "aa", "aa"}
[play]
Drop elements from the end of a slice or array while the predicate returns true.
l:=lo.DropRightWhile([]string{"a","aa","aaa","aa","aa"},func(valstring)bool {returnlen(val)<=2})// []string{"a", "aa", "aaa"}
[play]
Drops elements from a slice or array by the index. A negative index will drop elements from the end of the slice.
l:=lo.DropByIndex([]int{0,1,2,3,4,5},2,4,-1)// []int{0, 1, 3}
[play]
The opposite of Filter, this method returns the elements of collection that predicate does not return truthy for.
odd:=lo.Reject([]int{1,2,3,4},func(xint,_int)bool {returnx%2==0})// []int{1, 3}
[play]
The opposite of FilterMap, this method 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.
items:=lo.RejectMap([]int{1,2,3,4},func(xint,_int) (int,bool) {returnx*10,x%2==0})// []int{10, 30}
Mixes Filter and Reject, this method returns two slices, one for the elements of collection that predicate returns truthy for and one for the elements that predicate does not return truthy for.
kept,rejected:=lo.FilterReject([]int{1,2,3,4},func(xint,_int)bool {returnx%2==0})// []int{2, 4}// []int{1, 3}
Counts the number of elements in the collection that compare equal to value.
count:=lo.Count([]int{1,5,1},1)// 2
[play]
Counts the number of elements in the collection for which predicate is true.
count:=lo.CountBy([]int{1,5,1},func(iint)bool {returni<4})// 2
[play]
Counts the number of each element in the collection.
lo.CountValues([]int{})// map[int]int{}lo.CountValues([]int{1,2})// map[int]int{1: 1, 2: 1}lo.CountValues([]int{1,2,2})// map[int]int{1: 1, 2: 2}lo.CountValues([]string{"foo","bar",""})// map[string]int{"": 1, "foo": 1, "bar": 1}lo.CountValues([]string{"foo","bar","bar"})// map[string]int{"foo": 1, "bar": 2}
[play]
Counts the number of each element in the collection. It ss equivalent to chaining lo.Map and lo.CountValues.
isEven:=func(vint)bool {returnv%2==0}lo.CountValuesBy([]int{},isEven)// map[bool]int{}lo.CountValuesBy([]int{1,2},isEven)// map[bool]int{false: 1, true: 1}lo.CountValuesBy([]int{1,2,2},isEven)// map[bool]int{false: 1, true: 2}length:=func(vstring)int {returnlen(v)}lo.CountValuesBy([]string{"foo","bar",""},length)// map[int]int{0: 1, 3: 2}lo.CountValuesBy([]string{"foo","bar","bar"},length)// map[int]int{3: 3}
[play]
Returns a copy of a slice fromoffset
up tolength
elements. Likeslice[start:start+length]
, but does not panic on overflow.
in:= []int{0,1,2,3,4}sub:=lo.Subset(in,2,3)// []int{2, 3, 4}sub:=lo.Subset(in,-4,3)// []int{1, 2, 3}sub:=lo.Subset(in,-2,math.MaxUint)// []int{3, 4}
[play]
Returns a copy of a slice fromstart
up to, but not includingend
. Likeslice[start:end]
, but does not panic on overflow.
in:= []int{0,1,2,3,4}slice:=lo.Slice(in,0,5)// []int{0, 1, 2, 3, 4}slice:=lo.Slice(in,2,3)// []int{2}slice:=lo.Slice(in,2,6)// []int{2, 3, 4}slice:=lo.Slice(in,4,3)// []int{}
[play]
Returns a copy of the slice with the first n non-overlapping instances of old replaced by new.
in:= []int{0,1,0,1,2,3,0}slice:=lo.Replace(in,0,42,1)// []int{42, 1, 0, 1, 2, 3, 0}slice:=lo.Replace(in,-1,42,1)// []int{0, 1, 0, 1, 2, 3, 0}slice:=lo.Replace(in,0,42,2)// []int{42, 1, 42, 1, 2, 3, 0}slice:=lo.Replace(in,0,42,-1)// []int{42, 1, 42, 1, 2, 3, 42}
[play]
Returns a copy of the slice with all non-overlapping instances of old replaced by new.
in:= []int{0,1,0,1,2,3,0}slice:=lo.ReplaceAll(in,0,42)// []int{42, 1, 42, 1, 2, 3, 42}slice:=lo.ReplaceAll(in,-1,42)// []int{0, 1, 0, 1, 2, 3, 0}
[play]
Returns a slice of all non-zero elements.
in:= []string{"","foo","","bar",""}slice:=lo.Compact(in)// []string{"foo", "bar"}
[play]
Checks if a slice is sorted.
slice:=lo.IsSorted([]int{0,1,2,3,4,5,6,7,8,9})// true
[play]
Checks if a slice is sorted by iteratee.
slice:=lo.IsSortedByKey([]string{"a","bb","ccc"},func(sstring)int {returnlen(s)})// true
[play]
Splice inserts multiple elements at index i. A negative index counts back from the end of the slice. The helper is protected against overflow errors.
result:=lo.Splice([]string{"a","b"},1,"1","2")// []string{"a", "1", "2", "b"}// negativeresult=lo.Splice([]string{"a","b"},-1,"1","2")// []string{"a", "1", "2", "b"}// overflowresult=lo.Splice([]string{"a","b"},42,"1","2")// []string{"a", "b", "1", "2"}
[play]
Creates a slice of the map keys.
Use the UniqKeys variant to deduplicate common keys.
keys:=lo.Keys(map[string]int{"foo":1,"bar":2})// []string{"foo", "bar"}keys:=lo.Keys(map[string]int{"foo":1,"bar":2},map[string]int{"baz":3})// []string{"foo", "bar", "baz"}keys:=lo.Keys(map[string]int{"foo":1,"bar":2},map[string]int{"bar":3})// []string{"foo", "bar", "bar"}
[play]
Creates an array of unique map keys.
keys:=lo.UniqKeys(map[string]int{"foo":1,"bar":2},map[string]int{"baz":3})// []string{"foo", "bar", "baz"}keys:=lo.UniqKeys(map[string]int{"foo":1,"bar":2},map[string]int{"bar":3})// []string{"foo", "bar"}
[play]
Returns whether the given key exists.
exists:=lo.HasKey(map[string]int{"foo":1,"bar":2},"foo")// trueexists:=lo.HasKey(map[string]int{"foo":1,"bar":2},"baz")// false
[play]
Creates an array of the map values.
Use the UniqValues variant to deduplicate common values.
values:=lo.Values(map[string]int{"foo":1,"bar":2})// []int{1, 2}values:=lo.Values(map[string]int{"foo":1,"bar":2},map[string]int{"baz":3})// []int{1, 2, 3}values:=lo.Values(map[string]int{"foo":1,"bar":2},map[string]int{"bar":2})// []int{1, 2, 2}
[play]
Creates an array of unique map values.
values:=lo.UniqValues(map[string]int{"foo":1,"bar":2})// []int{1, 2}values:=lo.UniqValues(map[string]int{"foo":1,"bar":2},map[string]int{"baz":3})// []int{1, 2, 3}values:=lo.UniqValues(map[string]int{"foo":1,"bar":2},map[string]int{"bar":2})// []int{1, 2}
[play]
Returns the value of the given key or the fallback value if the key is not present.
value:=lo.ValueOr(map[string]int{"foo":1,"bar":2},"foo",42)// 1value:=lo.ValueOr(map[string]int{"foo":1,"bar":2},"baz",42)// 42
[play]
Returns same map type filtered by given predicate.
m:=lo.PickBy(map[string]int{"foo":1,"bar":2,"baz":3},func(keystring,valueint)bool {returnvalue%2==1})// map[string]int{"foo": 1, "baz": 3}
[play]
Returns same map type filtered by given keys.
m:=lo.PickByKeys(map[string]int{"foo":1,"bar":2,"baz":3}, []string{"foo","baz"})// map[string]int{"foo": 1, "baz": 3}
[play]
Returns same map type filtered by given values.
m:=lo.PickByValues(map[string]int{"foo":1,"bar":2,"baz":3}, []int{1,3})// map[string]int{"foo": 1, "baz": 3}
[play]
Returns same map type filtered by given predicate.
m:=lo.OmitBy(map[string]int{"foo":1,"bar":2,"baz":3},func(keystring,valueint)bool {returnvalue%2==1})// map[string]int{"bar": 2}
[play]
Returns same map type filtered by given keys.
m:=lo.OmitByKeys(map[string]int{"foo":1,"bar":2,"baz":3}, []string{"foo","baz"})// map[string]int{"bar": 2}
[play]
Returns same map type filtered by given values.
m:=lo.OmitByValues(map[string]int{"foo":1,"bar":2,"baz":3}, []int{1,3})// map[string]int{"bar": 2}
[play]
Transforms a map into array of key/value pairs.
entries:=lo.Entries(map[string]int{"foo":1,"bar":2})// []lo.Entry[string, int]{// {// Key: "foo",// Value: 1,// },// {// Key: "bar",// Value: 2,// },// }
[play]
Transforms an array of key/value pairs into a map.
m:=lo.FromEntries([]lo.Entry[string,int]{ {Key:"foo",Value:1, }, {Key:"bar",Value:2, },})// map[string]int{"foo": 1, "bar": 2}
[play]
Creates a map composed of the inverted keys and values. If map contains duplicate values, subsequent values overwrite property assignments of previous values.
m1:=lo.Invert(map[string]int{"a":1,"b":2})// map[int]string{1: "a", 2: "b"}m2:=lo.Invert(map[string]int{"a":1,"b":2,"c":1})// map[int]string{1: "c", 2: "b"}
[play]
Merges multiple maps from left to right.
mergedMaps:=lo.Assign(map[string]int{"a":1,"b":2},map[string]int{"b":3,"c":4},)// map[string]int{"a": 1, "b": 3, "c": 4}
[play]
Splits a map into an array of elements in groups of a length equal to its size. If the map cannot be split evenly, the final chunk will contain the remaining elements.
maps:=lo.ChunkEntries(map[string]int{"a":1,"b":2,"c":3,"d":4,"e":5, },3,)// []map[string]int{// {"a": 1, "b": 2, "c": 3},// {"d": 4, "e": 5},// }
Manipulates a map keys and transforms it to a map of another type.
m2:=lo.MapKeys(map[int]int{1:1,2:2,3:3,4:4},func(_int,vint)string {returnstrconv.FormatInt(int64(v),10)})// map[string]int{"1": 1, "2": 2, "3": 3, "4": 4}
[play]
Manipulates a map values and transforms it to a map of another type.
m1:=map[int]int64{1:1,2:2,3:3}m2:=lo.MapValues(m1,func(xint64,_int)string {returnstrconv.FormatInt(x,10)})// map[int]string{1: "1", 2: "2", 3: "3"}
[play]
Manipulates a map entries and transforms it to a map of another type.
in:=map[string]int{"foo":1,"bar":2}out:=lo.MapEntries(in,func(kstring,vint) (int,string) {returnv,k})// map[int]string{1: "foo", 2: "bar"}
[play]
Transforms a map into a slice based on specific iteratee.
m:=map[int]int64{1:4,2:5,3:6}s:=lo.MapToSlice(m,func(kint,vint64)string {returnfmt.Sprintf("%d_%d",k,v)})// []string{"1_4", "2_5", "3_6"}
[play]
Creates an array of numbers (positive and/or negative) progressing from start up to, but not including end.
result:=lo.Range(4)// [0, 1, 2, 3]result:=lo.Range(-4)// [0, -1, -2, -3]result:=lo.RangeFrom(1,5)// [1, 2, 3, 4, 5]result:=lo.RangeFrom[float64](1.0,5)// [1.0, 2.0, 3.0, 4.0, 5.0]result:=lo.RangeWithSteps(0,20,5)// [0, 5, 10, 15]result:=lo.RangeWithSteps[float32](-1.0,-4.0,-1.0)// [-1.0, -2.0, -3.0]result:=lo.RangeWithSteps(1,4,-1)// []result:=lo.Range(0)// []
[play]
Clamps number within the inclusive lower and upper bounds.
r1:=lo.Clamp(0,-10,10)// 0r2:=lo.Clamp(-42,-10,10)// -10r3:=lo.Clamp(42,-10,10)// 10
[play]
Sums the values in a collection.
If collection is empty 0 is returned.
list:= []int{1,2,3,4,5}sum:=lo.Sum(list)// 15
[play]
Summarizes the values in a collection using the given return value from the iteration function.
If collection is empty 0 is returned.
strings:= []string{"foo","bar"}sum:=lo.SumBy(strings,func(itemstring)int {returnlen(item)})// 6
Calculates the product of the values in a collection.
If collection is empty 0 is returned.
list:= []int{1,2,3,4,5}product:=lo.Product(list)// 120
[play]
Calculates the product of the values in a collection using the given return value from the iteration function.
If collection is empty 0 is returned.
strings:= []string{"foo","bar"}product:=lo.ProductBy(strings,func(itemstring)int {returnlen(item)})// 9
[play]
Calculates the mean of a collection of numbers.
If collection is empty 0 is returned.
mean:=lo.Mean([]int{2,3,4,5})// 3mean:=lo.Mean([]float64{2,3,4,5})// 3.5mean:=lo.Mean([]float64{})// 0
Calculates the mean of a collection of numbers using the given return value from the iteration function.
If collection is empty 0 is returned.
list:= []string{"aa","bbb","cccc","ddddd"}mapper:=func(itemstring)float64 {returnfloat64(len(item))}mean:=lo.MeanBy(list,mapper)// 3.5mean:=lo.MeanBy([]float64{},mapper)// 0
Returns a random string of the specified length and made of the specified charset.
str:=lo.RandomString(5,lo.LettersCharset)// example: "eIGbt"
[play]
Return part of a string.
sub:=lo.Substring("hello",2,3)// "llo"sub:=lo.Substring("hello",-4,3)// "ell"sub:=lo.Substring("hello",-2,math.MaxUint)// "lo"
[play]
Returns an array of strings split into groups the length of size. If array can't be split evenly, the final chunk will be the remaining elements.
lo.ChunkString("123456",2)// []string{"12", "34", "56"}lo.ChunkString("1234567",2)// []string{"12", "34", "56", "7"}lo.ChunkString("",2)// []string{""}lo.ChunkString("1",2)// []string{"1"}
[play]
An alias to utf8.RuneCountInString which returns the number of runes in string.
sub:=lo.RuneLength("hellô")// 5sub:=len("hellô")// 6
[play]
Converts string to pascal case.
str:=lo.PascalCase("hello_world")// HelloWorld
[play]
Converts string to camel case.
str:=lo.CamelCase("hello_world")// helloWorld
[play]
Converts string to kebab case.
str:=lo.KebabCase("helloWorld")// hello-world
[play]
Converts string to snake case.
str:=lo.SnakeCase("HelloWorld")// hello_world
[play]
Splits string into an array of its words.
str:=lo.Words("helloWorld")// []string{"hello", "world"}
[play]
Converts the first character of string to upper case and the remaining to lower case.
str:=lo.Capitalize("heLLO")// Hello
Trims and truncates a string to a specified length and appends an ellipsis if truncated.
str:=lo.Ellipsis(" Lorem Ipsum ",5)// Lo...str:=lo.Ellipsis("Lorem Ipsum",100)// Lorem Ipsumstr:=lo.Ellipsis("Lorem Ipsum",3)// ...
Creates a tuple from a list of values.
tuple1:=lo.T2("x",1)// Tuple2[string, int]{A: "x", B: 1}funcexample() (string,int) {return"y",2 }tuple2:=lo.T2(example())// Tuple2[string, int]{A: "y", B: 2}
[play]
Returns values contained in tuple.
r1,r2:=lo.Unpack2(lo.Tuple2[string,int]{"a",1})// "a", 1
Unpack is also available as a method of TupleX.
tuple2:=lo.T2("a",1)a,b:=tuple2.Unpack()// "a", 1
[play]
Zip creates a slice of grouped elements, the first of which contains the first elements of the given arrays, the second of which contains the second elements of the given arrays, and so on.
When collections have different size, the Tuple attributes are filled with zero value.
tuples:=lo.Zip2([]string{"a","b"}, []int{1,2})// []Tuple2[string, int]{{A: "a", B: 1}, {A: "b", B: 2}}
[play]
ZipBy creates a slice of transformed elements, the first of which contains the first elements of the given arrays, the second of which contains the second elements of the given arrays, and so on.
When collections have different size, the Tuple attributes are filled with zero value.
items:=lo.ZipBy2([]string{"a","b"}, []int{1,2},func(astring,bint)string {returnfmt.Sprintf("%s-%d",a,b)})// []string{"a-1", "b-2"}
Unzip accepts an array of grouped elements and creates an array regrouping the elements to their pre-zip configuration.
a,b:=lo.Unzip2([]Tuple2[string,int]{{A:"a",B:1}, {A:"b",B:2}})// []string{"a", "b"}// []int{1, 2}
[play]
UnzipBy2 iterates over a collection and creates an array regrouping the elements to their pre-zip configuration.
a,b:=lo.UnzipBy2([]string{"hello","john","doe"},func(strstring) (string,int) {returnstr,len(str)})// []string{"hello", "john", "doe"}// []int{5, 4, 3}
Combines every items from one list with every items from others. It is the cartesian product of lists received as arguments. It returns an empty list if a list is empty.
result:=lo.CrossJoin2([]string{"hello","john","doe"}, []int{1,2})// lo.Tuple2{"hello", 1}// lo.Tuple2{"hello", 2}// lo.Tuple2{"john", 1}// lo.Tuple2{"john", 2}// lo.Tuple2{"doe", 1}// lo.Tuple2{"doe", 2}
Combines every items from one list with every items from others. It is the cartesian product of lists received as arguments. The project function is used to create the output values. It returns an empty list if a list is empty.
result:=lo.CrossJoinBy2([]string{"hello","john","doe"}, []int{1,2},func(aA,bB)string {returnfmt.Sprintf("%s - %d",a,b)})// "hello - 1"// "hello - 2"// "john - 1"// "john - 2"// "doe - 1"// "doe - 2"
Returns the time taken to execute a function.
duration:=lo.Duration(func() {// very long job})// 3s
Returns the time taken to execute a function.
duration:=lo.Duration0(func() {// very long job})// 3serr,duration:=lo.Duration1(func()error {// very long jobreturnfmt.Errorf("an error")})// an error// 3sstr,nbr,err,duration:=lo.Duration3(func() (string,int,error) {// very long jobreturn"hello",42,nil})// hello// 42// nil// 3s
Distributes messages from input channels into N child channels. Close events are propagated to children.
Underlying channels can have a fixed buffer capacity or be unbuffered when cap is 0.
ch:=make(chanint,42)fori:=0;i<=10;i++ {ch<-i}children:=lo.ChannelDispatcher(ch,5,10,DispatchingStrategyRoundRobin[int])// []<-chan int{...}consumer:=func(c<-chanint) {for {msg,ok:=<-cif!ok {println("closed")break }println(msg) }}fori:=rangechildren {goconsumer(children[i])}
Many distributions strategies are available:
- lo.DispatchingStrategyRoundRobin: Distributes messages in a rotating sequential manner.
- lo.DispatchingStrategyRandom: Distributes messages in a random manner.
- lo.DispatchingStrategyWeightedRandom: Distributes messages in a weighted manner.
- lo.DispatchingStrategyFirst: Distributes messages in the first non-full channel.
- lo.DispatchingStrategyLeast: Distributes messages in the emptiest channel.
- lo.DispatchingStrategyMost: Distributes to the fullest channel.
Some strategies bring fallback, in order to favor non-blocking behaviors. See implementations.
For custom strategies, just implement thelo.DispatchingStrategy
prototype:
typeDispatchingStrategy[Tany]func(messageT,messageIndexuint64,channels []<-chanT)int
Eg:
typeMessagestruct {TenantID uuid.UUID}funchash(id uuid.UUID)int {h:=fnv.New32a()h.Write([]byte(id.String()))returnint(h.Sum32())}// Routes messages per TenantID.customStrategy:=func(messagestring,messageIndexuint64,channels []<-chanstring)int {destination:=hash(message)%len(channels)// check if channel is fulliflen(channels[destination])<cap(channels[destination]) {returndestination }// fallback when child channel is fullreturnutils.DispatchingStrategyRoundRobin(message,uint64(destination),channels)}children:=lo.ChannelDispatcher(ch,5,10,customStrategy)...
Returns a read-only channels of collection elements. Channel is closed after last element. Channel capacity can be customized.
list:= []int{1,2,3,4,5}forv:=rangelo.SliceToChannel(2,list) {println(v)}// prints 1, then 2, then 3, then 4, then 5
Returns a slice built from channels items. Blocks until channel closes.
list:= []int{1,2,3,4,5}ch:=lo.SliceToChannel(2,list)items:=ChannelToSlice(ch)// []int{1, 2, 3, 4, 5}
Implements the generator design pattern. Channel is closed after last element. Channel capacity can be customized.
generator:=func(yieldfunc(int)) {yield(1)yield(2)yield(3)}forv:=rangelo.Generator(2,generator) {println(v)}// prints 1, then 2, then 3
Creates a slice of n elements from a channel. Returns the slice, the slice length, the read time and the channel status (opened/closed).
ch:=lo.SliceToChannel(2, []int{1,2,3,4,5})items1,length1,duration1,ok1:=lo.Buffer(ch,3)// []int{1, 2, 3}, 3, 0s, trueitems2,length2,duration2,ok2:=lo.Buffer(ch,3)// []int{4, 5}, 2, 0s, false
Example: RabbitMQ consumer 👇
ch:=readFromQueue()for {// read 1k itemsitems,length,_,ok:=lo.Buffer(ch,1000)// do batching stuffif!ok {break }}
Creates a slice of n elements from a channel, with timeout. Returns the slice, the slice length, the read time and the channel status (opened/closed).
ctx,cancel:=context.WithCancel(context.TODO())gofunc() {ch<-0time.Sleep(10*time.Millisecond)ch<-1time.Sleep(10*time.Millisecond)ch<-2time.Sleep(10*time.Millisecond)ch<-3time.Sleep(10*time.Millisecond)ch<-4time.Sleep(10*time.Millisecond)cancel()}()items1,length1,duration1,ok1:=lo.BufferWithContext(ctx,ch,3)// []int{0, 1, 2}, 3, 20ms, trueitems2,length2,duration2,ok2:=lo.BufferWithContext(ctx,ch,3)// []int{3, 4}, 2, 30ms, false
Creates a slice of n elements from a channel, with timeout. Returns the slice, the slice length, the read time and the channel status (opened/closed).
generator:=func(yieldfunc(int)) {fori:=0;i<5;i++ {yield(i)time.Sleep(35*time.Millisecond) }}ch:=lo.Generator(0,generator)items1,length1,duration1,ok1:=lo.BufferWithTimeout(ch,3,100*time.Millisecond)// []int{1, 2}, 2, 100ms, trueitems2,length2,duration2,ok2:=lo.BufferWithTimeout(ch,3,100*time.Millisecond)// []int{3, 4, 5}, 3, 75ms, trueitems3,length3,duration2,ok3:=lo.BufferWithTimeout(ch,3,100*time.Millisecond)// []int{}, 0, 10ms, false
Example: RabbitMQ consumer 👇
ch:=readFromQueue()for {// read 1k items// wait up to 1 seconditems,length,_,ok:=lo.BufferWithTimeout(ch,1000,1*time.Second)// do batching stuffif!ok {break }}
Example: Multithreaded RabbitMQ consumer 👇
ch:=readFromQueue()// 5 workers// prefetch 1k messages per workerchildren:=lo.ChannelDispatcher(ch,5,1000,lo.DispatchingStrategyFirst[int])consumer:=func(c<-chanint) {for {// read 1k items// wait up to 1 seconditems,length,_,ok:=lo.BufferWithTimeout(ch,1000,1*time.Second)// do batching stuffif!ok {break } }}fori:=rangechildren {goconsumer(children[i])}
Merge messages from multiple input channels into a single buffered channel. Output messages has no priority. When all upstream channels reach EOF, downstream channel closes.
stream1:=make(chanint,42)stream2:=make(chanint,42)stream3:=make(chanint,42)all:=lo.FanIn(100,stream1,stream2,stream3)// <-chan int
Broadcasts all the upstream messages to multiple downstream channels. When upstream channel reach EOF, downstream channels close. If any downstream channels is full, broadcasting is paused.
stream:=make(chanint,42)all:=lo.FanOut(5,100,stream)// [5]<-chan int
Returns true if an element is present in a collection.
present:=lo.Contains([]int{0,1,2,3,4,5},5)// true
Returns true if the predicate function returnstrue
.
present:=lo.ContainsBy([]int{0,1,2,3,4,5},func(xint)bool {returnx==3})// true
Returns true if all elements of a subset are contained into a collection or if the subset is empty.
ok:=lo.Every([]int{0,1,2,3,4,5}, []int{0,2})// trueok:=lo.Every([]int{0,1,2,3,4,5}, []int{0,6})// false
Returns true if the predicate returns true for all elements in the collection or if the collection is empty.
b:=EveryBy([]int{1,2,3,4},func(xint)bool {returnx<5})// true
Returns true if at least 1 element of a subset is contained into a collection.If the subset is empty Some returns false.
ok:=lo.Some([]int{0,1,2,3,4,5}, []int{0,6})// trueok:=lo.Some([]int{0,1,2,3,4,5}, []int{-1,6})// false
Returns true if the predicate returns true for any of the elements in the collection.If the collection is empty SomeBy returns false.
b:=SomeBy([]int{1,2,3,4},func(xint)bool {returnx<3})// true
Returns true if no element of a subset are contained into a collection or if the subset is empty.
b:=None([]int{0,1,2,3,4,5}, []int{0,2})// falseb:=None([]int{0,1,2,3,4,5}, []int{-1,6})// true
Returns true if the predicate returns true for none of the elements in the collection or if the collection is empty.
b:=NoneBy([]int{1,2,3,4},func(xint)bool {returnx<0})// true
Returns the intersection between two collections.
result1:=lo.Intersect([]int{0,1,2,3,4,5}, []int{0,2})// []int{0, 2}result2:=lo.Intersect([]int{0,1,2,3,4,5}, []int{0,6})// []int{0}result3:=lo.Intersect([]int{0,1,2,3,4,5}, []int{-1,6})// []int{}
Returns the difference between two collections.
- The first value is the collection of element absent of list2.
- The second value is the collection of element absent of list1.
left,right:=lo.Difference([]int{0,1,2,3,4,5}, []int{0,2,6})// []int{1, 3, 4, 5}, []int{6}left,right:=lo.Difference([]int{0,1,2,3,4,5}, []int{0,1,2,3,4,5})// []int{}, []int{}
Returns all distinct elements from given collections. Result will not change the order of elements relatively.
union:=lo.Union([]int{0,1,2,3,4,5}, []int{0,2}, []int{0,10})// []int{0, 1, 2, 3, 4, 5, 10}
Returns slice excluding all given values.
subset:=lo.Without([]int{0,2,10},2)// []int{0, 10}subset:=lo.Without([]int{0,2,10},0,1,2,3,4,5)// []int{10}
Filters a slice by excluding elements whose extracted keys match any in the exclude list.
It returns a new slice containing only the elements whose keys are not in the exclude list.
typestructUser {IDintNamestring}// original usersusers:= []User{ {ID:1,Name:"Alice"}, {ID:2,Name:"Bob"}, {ID:3,Name:"Charlie"},}// extract function to get the user IDgetID:=func(userUser)int {returnuser.ID}// exclude users with IDs 2 and 3excludedIDs:= []int{2,3}// filtering usersfilteredUsers:=lo.WithoutBy(users,getID,excludedIDs...)// []User[{ID: 1, Name: "Alice"}]
Returns slice excluding zero values.
subset:=lo.WithoutEmpty([]int{0,2,10})// []int{2, 10}
Returns slice excluding nth value.
subset:=lo.WithoutNth([]int{-2,-1,0,1,2},3,-42,1)// []int{-2, 0, 2}
Returns the index at which the first occurrence of a value is found in an array or return -1 if the value cannot be found.
found:=lo.IndexOf([]int{0,1,2,1,2,3},2)// 2notFound:=lo.IndexOf([]int{0,1,2,1,2,3},6)// -1
Returns the index at which the last occurrence of a value is found in an array or return -1 if the value cannot be found.
found:=lo.LastIndexOf([]int{0,1,2,1,2,3},2)// 4notFound:=lo.LastIndexOf([]int{0,1,2,1,2,3},6)// -1
Search an element in a slice based on a predicate. It returns element and true if element was found.
str,ok:=lo.Find([]string{"a","b","c","d"},func(istring)bool {returni=="b"})// "b", truestr,ok:=lo.Find([]string{"foobar"},func(istring)bool {returni=="b"})// "", false
FindIndexOf searches an element in a slice based on a predicate and returns the index and true. It returns -1 and false if the element is not found.
str,index,ok:=lo.FindIndexOf([]string{"a","b","a","b"},func(istring)bool {returni=="b"})// "b", 1, truestr,index,ok:=lo.FindIndexOf([]string{"foobar"},func(istring)bool {returni=="b"})// "", -1, false
FindLastIndexOf searches an element in a slice based on a predicate and returns the index and true. It returns -1 and false if the element is not found.
str,index,ok:=lo.FindLastIndexOf([]string{"a","b","a","b"},func(istring)bool {returni=="b"})// "b", 4, truestr,index,ok:=lo.FindLastIndexOf([]string{"foobar"},func(istring)bool {returni=="b"})// "", -1, false
Search an element in a slice based on a predicate. It returns the element if found or a given fallback value otherwise.
str:=lo.FindOrElse([]string{"a","b","c","d"},"x",func(istring)bool {returni=="b"})// "b"str:=lo.FindOrElse([]string{"foobar"},"x",func(istring)bool {returni=="b"})// "x"
Returns the key of the first value matching.
result1,ok1:=lo.FindKey(map[string]int{"foo":1,"bar":2,"baz":3},2)// "bar", trueresult2,ok2:=lo.FindKey(map[string]int{"foo":1,"bar":2,"baz":3},42)// "", falsetypeteststruct {foobarstring}result3,ok3:=lo.FindKey(map[string]test{"foo":test{"foo"},"bar":test{"bar"},"baz":test{"baz"}},test{"foo"})// "foo", true
Returns the key of the first element predicate returns truthy for.
result1,ok1:=lo.FindKeyBy(map[string]int{"foo":1,"bar":2,"baz":3},func(kstring,vint)bool {returnk=="foo"})// "foo", trueresult2,ok2:=lo.FindKeyBy(map[string]int{"foo":1,"bar":2,"baz":3},func(kstring,vint)bool {returnfalse})// "", false
Returns a slice with all the unique elements of the collection. The order of result values is determined by the order they occur in the array.
uniqueValues:=lo.FindUniques([]int{1,2,2,1,2,3})// []int{3}
Returns a slice with all the unique elements of the collection. The order of result values is determined by the order they occur in the array. It acceptsiteratee
which is invoked for each element in array to generate the criterion by which uniqueness is computed.
uniqueValues:=lo.FindUniquesBy([]int{3,4,5,6,7},func(iint)int {returni%3})// []int{5}
Returns a slice with the first occurrence of each duplicated elements of the collection. The order of result values is determined by the order they occur in the array.
duplicatedValues:=lo.FindDuplicates([]int{1,2,2,1,2,3})// []int{1, 2}
Returns a slice with the first occurrence of each duplicated elements of the collection. The order of result values is determined by the order they occur in the array. It acceptsiteratee
which is invoked for each element in array to generate the criterion by which uniqueness is computed.
duplicatedValues:=lo.FindDuplicatesBy([]int{3,4,5,6,7},func(iint)int {returni%3})// []int{3, 4}
Search the minimum value of a collection.
Returns zero value when the collection is empty.
min:=lo.Min([]int{1,2,3})// 1min:=lo.Min([]int{})// 0min:=lo.Min([]time.Duration{time.Second,time.Hour})// 1s
Search the minimum value of a collection and the index of the minimum value.
Returns (zero value, -1) when the collection is empty.
min,index:=lo.MinIndex([]int{1,2,3})// 1, 0min,index:=lo.MinIndex([]int{})// 0, -1min,index:=lo.MinIndex([]time.Duration{time.Second,time.Hour})// 1s, 0
Search the minimum value of a collection using the given comparison function.
If several values of the collection are equal to the smallest value, returns the first such value.
Returns zero value when the collection is empty.
min:=lo.MinBy([]string{"s1","string2","s3"},func(itemstring,minstring)bool {returnlen(item)<len(min)})// "s1"min:=lo.MinBy([]string{},func(itemstring,minstring)bool {returnlen(item)<len(min)})// ""
Search the minimum value of a collection using the given comparison function and the index of the minimum value.
If several values of the collection are equal to the smallest value, returns the first such value.
Returns (zero value, -1) when the collection is empty.
min,index:=lo.MinIndexBy([]string{"s1","string2","s3"},func(itemstring,minstring)bool {returnlen(item)<len(min)})// "s1", 0min,index:=lo.MinIndexBy([]string{},func(itemstring,minstring)bool {returnlen(item)<len(min)})// "", -1
Search the minimum time.Time of a collection.
Returns zero value when the collection is empty.
earliest:=lo.Earliest(time.Now(), time.Time{})// 0001-01-01 00:00:00 +0000 UTC
Search the minimum time.Time of a collection using the given iteratee function.
Returns zero value when the collection is empty.
typefoostruct {bar time.Time}earliest:=lo.EarliestBy([]foo{{time.Now()}, {}},func(ifoo) time.Time {returni.bar})// {bar:{2023-04-01 01:02:03 +0000 UTC}}
Search the maximum value of a collection.
Returns zero value when the collection is empty.
max:=lo.Max([]int{1,2,3})// 3max:=lo.Max([]int{})// 0max:=lo.Max([]time.Duration{time.Second,time.Hour})// 1h
Search the maximum value of a collection and the index of the maximum value.
Returns (zero value, -1) when the collection is empty.
max,index:=lo.MaxIndex([]int{1,2,3})// 3, 2max,index:=lo.MaxIndex([]int{})// 0, -1max,index:=lo.MaxIndex([]time.Duration{time.Second,time.Hour})// 1h, 1
Search the maximum value of a collection using the given comparison function.
If several values of the collection are equal to the greatest value, returns the first such value.
Returns zero value when the collection is empty.
max:=lo.MaxBy([]string{"string1","s2","string3"},func(itemstring,maxstring)bool {returnlen(item)>len(max)})// "string1"max:=lo.MaxBy([]string{},func(itemstring,maxstring)bool {returnlen(item)>len(max)})// ""
Search the maximum value of a collection using the given comparison function and the index of the maximum value.
If several values of the collection are equal to the greatest value, returns the first such value.
Returns (zero value, -1) when the collection is empty.
max,index:=lo.MaxIndexBy([]string{"string1","s2","string3"},func(itemstring,maxstring)bool {returnlen(item)>len(max)})// "string1", 0max,index:=lo.MaxIndexBy([]string{},func(itemstring,maxstring)bool {returnlen(item)>len(max)})// "", -1
Search the maximum time.Time of a collection.
Returns zero value when the collection is empty.
latest:=lo.Latest([]time.Time{time.Now(), time.Time{}})// 2023-04-01 01:02:03 +0000 UTC
Search the maximum time.Time of a collection using the given iteratee function.
Returns zero value when the collection is empty.
typefoostruct {bar time.Time}latest:=lo.LatestBy([]foo{{time.Now()}, {}},func(ifoo) time.Time {returni.bar})// {bar:{2023-04-01 01:02:03 +0000 UTC}}
Returns the first element of a collection and check for availability of the first element.
first,ok:=lo.First([]int{1,2,3})// 1, truefirst,ok:=lo.First([]int{})// 0, false
Returns the first element of a collection or zero value if empty.
first:=lo.FirstOrEmpty([]int{1,2,3})// 1first:=lo.FirstOrEmpty([]int{})// 0
Returns the first element of a collection or the fallback value if empty.
first:=lo.FirstOr([]int{1,2,3},245)// 1first:=lo.FirstOr([]int{},31)// 31
Returns the last element of a collection or error if empty.
last,ok:=lo.Last([]int{1,2,3})// 3// truelast,ok:=lo.Last([]int{})// 0// false
Returns the first element of a collection or zero value if empty.
last:=lo.LastOrEmpty([]int{1,2,3})// 3last:=lo.LastOrEmpty([]int{})// 0
Returns the first element of a collection or the fallback value if empty.
last:=lo.LastOr([]int{1,2,3},245)// 3last:=lo.LastOr([]int{},31)// 31
Returns the element at indexnth
of collection. Ifnth
is negative, the nth element from the end is returned. An error is returned when nth is out of slice bounds.
nth,err:=lo.Nth([]int{0,1,2,3},2)// 2nth,err:=lo.Nth([]int{0,1,2,3},-2)// 2
Returns the element at indexnth
of the collection. Ifnth
is negative, it returns thenth
element from the end. Ifnth
is out of slice bounds, it returns the provided fallback value
nth:=lo.NthOr([]int{10,20,30,40,50},2,-1)// 30nth:=lo.NthOr([]int{10,20,30,40,50},-1,-1)// 50nth:=lo.NthOr([]int{10,20,30,40,50},5,-1)// -1 (fallback value)
Returns the element at indexnth
of the collection. Ifnth
is negative, it returns thenth
element from the end. Ifnth
is out of slice bounds, it returns the zero value for the element type (e.g., 0 for integers, "" for strings, etc).
nth:=lo.NthOrEmpty([]int{10,20,30,40,50},2)// 30nth:=lo.NthOrEmpty([]int{10,20,30,40,50},-1)// 50nth:=lo.NthOrEmpty([]int{10,20,30,40,50},5)// 0 (zero value for int)nth:=lo.NthOrEmpty([]string{"apple","banana","cherry"},2)// "cherry"nth:=lo.NthOrEmpty([]string{"apple","banana","cherry"},5)// "" (zero value for string)
Returns a random item from collection.
lo.Sample([]string{"a","b","c"})// a random string from []string{"a", "b", "c"}lo.Sample([]string{})// ""
Returns a random item from collection, using a given random integer generator.
import"math/rand"r:=rand.New(rand.NewSource(42))lo.SampleBy([]string{"a","b","c"},r.Intn)// a random string from []string{"a", "b", "c"}, using a seeded random generatorlo.SampleBy([]string{},r.Intn)// ""
Returns N random unique items from collection.
lo.Samples([]string{"a","b","c"},3)// []string{"a", "b", "c"} in random order
Returns N random unique items from collection, using a given random integer generator.
r:=rand.New(rand.NewSource(42))lo.SamplesBy([]string{"a","b","c"},3,r.Intn)// []string{"a", "b", "c"} in random order, using a seeded random generator
A 1 line if/else statement.
result:=lo.Ternary(true,"a","b")// "a"result:=lo.Ternary(false,"a","b")// "b"
Take care to avoid dereferencing potentially nil pointers in your A/B expressions, because they are both evaluated. See TernaryF to avoid this problem.
[play]
A 1 line if/else statement whose options are functions.
result:=lo.TernaryF(true,func()string {return"a" },func()string {return"b" })// "a"result:=lo.TernaryF(false,func()string {return"a" },func()string {return"b" })// "b"
Useful to avoid nil-pointer dereferencing in initializations, or avoid running unnecessary code
vars*stringsomeStr:=TernaryF(s==nil,func()string {returnuuid.New().String() },func()string {return*s })// ef782193-c30c-4e2e-a7ae-f8ab5e125e02
[play]
result:=lo.If(true,1).ElseIf(false,2).Else(3)// 1result:=lo.If(false,1).ElseIf(true,2).Else(3)// 2result:=lo.If(false,1).ElseIf(false,2).Else(3)// 3
Using callbacks:
result:=lo.IfF(true,func ()int {return1 }).ElseIfF(false,func ()int {return2 }).ElseF(func ()int {return3 })// 1
Mixed:
result:=lo.IfF(true,func ()int {return1 }).Else(42)// 1
[play]
result:=lo.Switch(1).Case(1,"1").Case(2,"2").Default("3")// "1"result:=lo.Switch(2).Case(1,"1").Case(2,"2").Default("3")// "2"result:=lo.Switch(42).Case(1,"1").Case(2,"2").Default("3")// "3"
Using callbacks:
result:=lo.Switch(1).CaseF(1,func()string {return"1" }).CaseF(2,func()string {return"2" }).DefaultF(func()string {return"3" })// "1"
Mixed:
result:=lo.Switch(1).CaseF(1,func()string {return"1" }).Default("42")// "1"
[play]
Checks if a value is nil or if it's a reference type with a nil underlying value.
varxintlo.IsNil(x)// falsevarkstruct{}lo.IsNil(k)// falsevari*intlo.IsNil(i)// truevarifaceWithNilValueany= (*string)(nil)lo.IsNil(ifaceWithNilValue)// trueifaceWithNilValue==nil// false
Checks if a value is not nil or if it's not a reference type with a nil underlying value.
varxintlo.IsNotNil(x)// truevarkstruct{}lo.IsNotNil(k)// truevari*intlo.IsNotNil(i)// falsevarifaceWithNilValueany= (*string)(nil)lo.IsNotNil(ifaceWithNilValue)// falseifaceWithNilValue==nil// true
Returns a pointer copy of the value.
ptr:=lo.ToPtr("hello world")// *string{"hello world"}
Returns a nil pointer of type.
ptr:=lo.Nil[float64]()// nil
Returns a pointer copy of value if it's nonzero.Otherwise, returns nil pointer.
ptr:=lo.EmptyableToPtr(nil)// nilptr:=lo.EmptyableToPtr("")// nilptr:=lo.EmptyableToPtr([]int{})// *[]int{}ptr:=lo.EmptyableToPtr("hello world")// *string{"hello world"}
Returns the pointer value or empty.
str:="hello world"value:=lo.FromPtr(&str)// "hello world"value:=lo.FromPtr(nil)// ""
Returns the pointer value or the fallback value.
str:="hello world"value:=lo.FromPtrOr(&str,"empty")// "hello world"value:=lo.FromPtrOr(nil,"empty")// "empty"
Returns a slice of pointer copy of value.
ptr:=lo.ToSlicePtr([]string{"hello","world"})// []*string{"hello", "world"}
Returns a slice with the pointer values.Returns a zero value in case of a nil pointer element.
str1:="hello"str2:="world"ptr:= lo.FromSlicePtr[string]([]*string{&str1,&str2,nil})// []string{"hello", "world", ""}ptr:=lo.Compact( lo.FromSlicePtr[string]([]*string{&str1,&str2,nil}),)// []string{"hello", "world"}
Returns a slice with the pointer values or the fallback value.
str1:="hello"str2:="world"ptr:=lo.FromSlicePtrOr([]*string{&str1,nil,&str2},"fallback value")// []string{"hello", "fallback value", "world"}
[play]
Returns a slice with all elements mapped toany
type.
elements:=lo.ToAnySlice([]int{1,5,1})// []any{1, 5, 1}
Returns anany
slice with all elements mapped to a type. Returns false in case of type conversion failure.
elements,ok:=lo.FromAnySlice([]any{"foobar",42})// []string{}, falseelements,ok:=lo.FromAnySlice([]any{"foobar","42"})// []string{"foobar", "42"}, true
Returns thezero value.
lo.Empty[int]()// 0lo.Empty[string]()// ""lo.Empty[bool]()// false
Returns true if argument is a zero value.
lo.IsEmpty(0)// truelo.IsEmpty(42)// falselo.IsEmpty("")// truelo.IsEmpty("foobar")// falsetypeteststruct {foobarstring}lo.IsEmpty(test{foobar:""})// truelo.IsEmpty(test{foobar:"foobar"})// false
Returns true if argument is a zero value.
lo.IsNotEmpty(0)// falselo.IsNotEmpty(42)// truelo.IsNotEmpty("")// falselo.IsNotEmpty("foobar")// truetypeteststruct {foobarstring}lo.IsNotEmpty(test{foobar:""})// falselo.IsNotEmpty(test{foobar:"foobar"})// true
Returns the first non-empty arguments. Arguments must be comparable.
result,ok:=lo.Coalesce(0,1,2,3)// 1 trueresult,ok:=lo.Coalesce("")// "" falsevarnilStr*stringstr:="foobar"result,ok:=lo.Coalesce(nil,nilStr,&str)// &"foobar" true
Returns the first non-empty arguments. Arguments must be comparable.
result:=lo.CoalesceOrEmpty(0,1,2,3)// 1result:=lo.CoalesceOrEmpty("")// ""varnilStr*stringstr:="foobar"result:=lo.CoalesceOrEmpty(nil,nilStr,&str)// &"foobar"
Returns the first non-zero slice.
result,ok:=lo.CoalesceSlice([]int{1,2,3}, []int{4,5,6})// [1, 2, 3]// trueresult,ok:=lo.CoalesceSlice(nil, []int{})// []// trueresult,ok:=lo.CoalesceSlice([]int(nil))// []// false
Returns the first non-zero slice.
result:=lo.CoalesceSliceOrEmpty([]int{1,2,3}, []int{4,5,6})// [1, 2, 3]result:=lo.CoalesceSliceOrEmpty(nil, []int{})// []
Returns the first non-zero map.
result,ok:=lo.CoalesceMap(map[string]int{"1":1,"2":2,"3":3},map[string]int{"4":4,"5":5,"6":6})// {"1": 1, "2": 2, "3": 3}// trueresult,ok:=lo.CoalesceMap(nil,map[string]int{})// {}// trueresult,ok:=lo.CoalesceMap(map[string]int(nil))// {}// false
Returns the first non-zero map.
result:=lo.CoalesceMapOrEmpty(map[string]int{"1":1,"2":2,"3":3},map[string]int{"4":4,"5":5,"6":6})// {"1": 1, "2": 2, "3": 3}result:=lo.CoalesceMapOrEmpty(nil,map[string]int{})// {}
Returns new function that, when called, has its first argument set to the provided value.
add:=func(x,yint)int {returnx+y }f:=lo.Partial(add,5)f(10)// 15f(42)// 47
Returns new function that, when called, has its first argument set to the provided value.
add:=func(x,y,zint)int {returnx+y+z }f:=lo.Partial2(add,42)f(10,5)// 57f(42,-4)// 80
Invokes a function N times until it returns valid output. Returns either the caught error or nil.
When the first argument is less than1
, the function runs until a successful response is returned.
iter,err:=lo.Attempt(42,func(iint)error {ifi==5 {returnnil }returnfmt.Errorf("failed")})// 6// niliter,err:=lo.Attempt(2,func(iint)error {ifi==5 {returnnil }returnfmt.Errorf("failed")})// 2// error "failed"iter,err:=lo.Attempt(0,func(iint)error {ifi<42 {returnfmt.Errorf("failed") }returnnil})// 43// nil
For more advanced retry strategies (delay, exponential backoff...), please take a look oncenkalti/backoff.
[play]
Invokes a function N times until it returns valid output, with a pause between each call. Returns either the caught error or nil.
When the first argument is less than1
, the function runs until a successful response is returned.
iter,duration,err:=lo.AttemptWithDelay(5,2*time.Second,func(iint,duration time.Duration)error {ifi==2 {returnnil }returnfmt.Errorf("failed")})// 3// ~ 4 seconds// nil
For more advanced retry strategies (delay, exponential backoff...), please take a look oncenkalti/backoff.
[play]
Invokes a function N times until it returns valid output. Returns either the caught error or nil, along with a bool value to determine whether the function should be invoked again. It will terminate the invoke immediately if the second return value is false.
When the first argument is less than1
, the function runs until a successful response is returned.
count1,err1:=lo.AttemptWhile(5,func(iint) (error,bool) {err:=doMockedHTTPRequest(i)iferr!=nil {iferrors.Is(err,ErrBadRequest) {// lets assume ErrBadRequest is a critical error that needs to terminate the invokereturnerr,false// flag the second return value as false to terminate the invoke }returnerr,true }returnnil,false})
For more advanced retry strategies (delay, exponential backoff...), please take a look oncenkalti/backoff.
[play]
Invokes a function N times until it returns valid output, with a pause between each call. Returns either the caught error or nil, along with a bool value to determine whether the function should be invoked again. It will terminate the invoke immediately if the second return value is false.
When the first argument is less than1
, the function runs until a successful response is returned.
count1,time1,err1:=lo.AttemptWhileWithDelay(5,time.Millisecond,func(iint,d time.Duration) (error,bool) {err:=doMockedHTTPRequest(i)iferr!=nil {iferrors.Is(err,ErrBadRequest) {// lets assume ErrBadRequest is a critical error that needs to terminate the invokereturnerr,false// flag the second return value as false to terminate the invoke }returnerr,true }returnnil,false})
For more advanced retry strategies (delay, exponential backoff...), please take a look oncenkalti/backoff.
[play]
NewDebounce
creates a debounced instance that delays invoking functions given until after wait milliseconds have elapsed, untilcancel
is called.
f:=func() {println("Called once after 100ms when debounce stopped invoking!")}debounce,cancel:=lo.NewDebounce(100*time.Millisecond,f)forj:=0;j<10;j++ {debounce()}time.Sleep(1*time.Second)cancel()
[play]
NewDebounceBy
creates a debounced instance for each distinct key, that delays invoking functions given until after wait milliseconds have elapsed, untilcancel
is called.
f:=func(keystring,countint) {println(key+": Called once after 100ms when debounce stopped invoking!")}debounce,cancel:=lo.NewDebounceBy(100*time.Millisecond,f)forj:=0;j<10;j++ {debounce("first key")debounce("second key")}time.Sleep(1*time.Second)cancel("first key")cancel("second key")
[play]
Creates a throttled instance that invokes given functions only once in every interval.
This returns 2 functions, First one is throttled function and Second one is a function to reset interval.
f:=func() {println("Called once in every 100ms")}throttle,reset:=lo.NewThrottle(100*time.Millisecond,f)forj:=0;j<10;j++ {throttle()time.Sleep(30*time.Millisecond)}reset()throttle()
NewThrottleWithCount
is NewThrottle with count limit, throttled function will be invoked count times in every interval.
f:=func() {println("Called three times in every 100ms")}throttle,reset:=lo.NewThrottleWithCount(100*time.Millisecond,f)forj:=0;j<10;j++ {throttle()time.Sleep(30*time.Millisecond)}reset()throttle()
NewThrottleBy
andNewThrottleByWithCount
are NewThrottle with sharding key, throttled function will be invoked count times in every interval.
f:=func(keystring) {println(key,"Called three times in every 100ms")}throttle,reset:=lo.NewThrottleByWithCount(100*time.Millisecond,f)forj:=0;j<10;j++ {throttle("foo")time.Sleep(30*time.Millisecond)}reset()throttle()
Wraps the underlying callback in a mutex. It receives an optional mutex.
s:=lo.Synchronize()fori:=0;i<10;i++ {gos.Do(func () {println("will be called sequentially") })}
It is equivalent to:
mu:= sync.Mutex{}funcfoobar() {mu.Lock()defermu.Unlock()// ...}
Executes a function in a goroutine and returns the result in a channel.
ch:=lo.Async(func()error {time.Sleep(10*time.Second);returnnil })// chan error (nil)
Executes a function in a goroutine and returns the result in a channel.For function with multiple return values, the results will be returned as a tuple inside the channel.For function without return, struct{} will be returned in the channel.
ch:=lo.Async0(func() {time.Sleep(10*time.Second) })// chan struct{}ch:=lo.Async1(func()int {time.Sleep(10*time.Second);return42})// chan int (42)ch:=lo.Async2(func() (int,string) {time.Sleep(10*time.Second);return42,"Hello"})// chan lo.Tuple2[int, string] ({42, "Hello"})
Implements a Saga pattern.
transaction:=NewTransaction().Then(func(stateint) (int,error) {fmt.Println("step 1")returnstate+10,nil },func(stateint)int {fmt.Println("rollback 1")returnstate-10 }, ).Then(func(stateint) (int,error) {fmt.Println("step 2")returnstate+15,nil },func(stateint)int {fmt.Println("rollback 2")returnstate-15 }, ).Then(func(stateint) (int,error) {fmt.Println("step 3")iftrue {returnstate,fmt.Errorf("error") }returnstate+42,nil },func(stateint)int {fmt.Println("rollback 3")returnstate-42 }, )_,_=transaction.Process(-5)// Output:// step 1// step 2// step 3// rollback 2// rollback 1
Runs periodically until a condition is validated.
alwaysTrue:=func(iint)bool {returntrue }alwaysFalse:=func(iint)bool {returnfalse }laterTrue:=func(iint)bool {returni>5}iterations,duration,ok:=lo.WaitFor(alwaysTrue,10*time.Millisecond,2*time.Millisecond)// 1// 1ms// trueiterations,duration,ok:=lo.WaitFor(alwaysFalse,10*time.Millisecond,time.Millisecond)// 10// 10ms// falseiterations,duration,ok:=lo.WaitFor(laterTrue,10*time.Millisecond,time.Millisecond)// 7// 7ms// trueiterations,duration,ok:=lo.WaitFor(laterTrue,10*time.Millisecond,5*time.Millisecond)// 2// 10ms// false
Runs periodically until a condition is validated or context is invalid.
The condition receives also the context, so it can invalidate the process in the condition checker
ctx:=context.Background()alwaysTrue:=func(_ context.Context,iint)bool {returntrue }alwaysFalse:=func(_ context.Context,iint)bool {returnfalse }laterTrue:=func(_ context.Context,iint)bool {returni>=5}iterations,duration,ok:=lo.WaitForWithContext(ctx,alwaysTrue,10*time.Millisecond,2*time.Millisecond)// 1// 1ms// trueiterations,duration,ok:=lo.WaitForWithContext(ctx,alwaysFalse,10*time.Millisecond,time.Millisecond)// 10// 10ms// falseiterations,duration,ok:=lo.WaitForWithContext(ctx,laterTrue,10*time.Millisecond,time.Millisecond)// 5// 5ms// trueiterations,duration,ok:=lo.WaitForWithContext(ctx,laterTrue,10*time.Millisecond,5*time.Millisecond)// 2// 10ms// falseexpiringCtx,cancel:=context.WithTimeout(ctx,5*time.Millisecond)iterations,duration,ok:=lo.WaitForWithContext(expiringCtx,alwaysFalse,100*time.Millisecond,time.Millisecond)// 5// 5.1ms// false
Helper function that creates an error when a condition is not met.
slice:= []string{"a"}val:=lo.Validate(len(slice)==0,"Slice should be empty but contains %v",slice)// error("Slice should be empty but contains [a]")slice:= []string{}val:=lo.Validate(len(slice)==0,"Slice should be empty but contains %v",slice)// nil
[play]
Wraps a function call to panics if second argument iserror
orfalse
, returns the value otherwise.
val:=lo.Must(time.Parse("2006-01-02","2022-01-15"))// 2022-01-15val:=lo.Must(time.Parse("2006-01-02","bad-value"))// panics
[play]
Must* has the same behavior as Must, but returns multiple values.
funcexample0() (error)funcexample1() (int,error)funcexample2() (int,string,error)funcexample3() (int,string, time.Date,error)funcexample4() (int,string, time.Date,bool,error)funcexample5() (int,string, time.Date,bool,float64,error)funcexample6() (int,string, time.Date,bool,float64,byte,error)lo.Must0(example0())val1:=lo.Must1(example1())// alias to Mustval1,val2:=lo.Must2(example2())val1,val2,val3:=lo.Must3(example3())val1,val2,val3,val4:=lo.Must4(example4())val1,val2,val3,val4,val5:=lo.Must5(example5())val1,val2,val3,val4,val5,val6:=lo.Must6(example6())
You can wrap functions likefunc (...) (..., ok bool)
.
// math.Signbit(float64) boollo.Must0(math.Signbit(v))// bytes.Cut([]byte,[]byte) ([]byte, []byte, bool)before,after:=lo.Must2(bytes.Cut(s,sep))
You can give context to the panic message by adding some printf-like arguments.
val,ok:=lo.Find(myString,func(istring)bool {returni==requiredChar})lo.Must0(ok,"'%s' must always contain '%s'",myString,requiredChar)list:= []int{0,1,2}item:=5lo.Must0(lo.Contains(list,item),"'%s' must always contain '%s'",list,item)...
[play]
Calls the function and returns false in case of error and panic.
ok:=lo.Try(func()error {panic("error")returnnil})// falseok:=lo.Try(func()error {returnnil})// trueok:=lo.Try(func()error {returnfmt.Errorf("error")})// false
[play]
The same behavior asTry
, but the callback returns 2 variables.
ok:=lo.Try2(func() (string,error) {panic("error")return"",nil})// false
[play]
Calls the function and return a default value in case of error and on panic.
str,ok:=lo.TryOr(func() (string,error) {panic("error")return"hello",nil},"world")// world// falsestr,ok:=lo.TryOr(func()error {return"hello",nil},"world")// hello// truestr,ok:=lo.TryOr(func()error {return"hello",fmt.Errorf("error")},"world")// world// false
[play]
The same behavior asTryOr
, but the callback returnsX
variables.
str,nbr,ok:=lo.TryOr2(func() (string,int,error) {panic("error")return"hello",42,nil},"world",21)// world// 21// false
[play]
The same behavior asTry
, but also returns the value passed to panic.
err,ok:=lo.TryWithErrorValue(func()error {panic("error")returnnil})// "error", false
[play]
The same behavior asTry
, but calls the catch function in case of error.
caught:=falseok:=lo.TryCatch(func()error {panic("error")returnnil},func() {caught=true})// false// caught == true
[play]
The same behavior asTryWithErrorValue
, but calls the catch function in case of error.
caught:=falseok:=lo.TryCatchWithErrorValue(func()error {panic("error")returnnil},func(valany) {caught=val=="error"})// false// caught == true
[play]
A shortcut for:
err:=doSomething()varrateLimitErr*RateLimitErrorifok:=errors.As(err,&rateLimitErr);ok {// retry later}
1 linelo
helper:
err:=doSomething()ifrateLimitErr,ok:= lo.ErrorsAs[*RateLimitError](err);ok {// retry later}
[play]
We executed a simple benchmark with a dead-simplelo.Map
loop:
See the full implementationhere.
_=lo.Map[int64](arr,func(xint64,iint)string {returnstrconv.FormatInt(x,10)})
Result:
Here is a comparison betweenlo.Map
,lop.Map
,go-funk
library and a simple Gofor
loop.
$ gotest -benchmem -bench ./...goos: linuxgoarch: amd64pkg: github.com/samber/locpu: Intel(R) Core(TM) i5-7267U CPU @ 3.10GHzcpu: Intel(R) Core(TM) i7 CPU 920 @ 2.67GHzBenchmarkMap/lo.Map-8 8 132728237 ns/op39998945 B/op 1000002 allocs/opBenchmarkMap/lop.Map-8 2 503947830 ns/op119999956 B/op 3000007 allocs/opBenchmarkMap/reflect-8 2 826400560 ns/op170326512 B/op 4000042 allocs/opBenchmarkMap/for-8 9 126252954 ns/op39998674 B/op 1000001 allocs/opPASSok github.com/samber/lo6.657s
lo.Map
is way faster (x7) thango-funk
, a reflection-based Map implementation.lo.Map
have the same allocation profile thanfor
.lo.Map
is 4% slower thanfor
.lop.Map
is slower thanlo.Map
because it implies more memory allocation and locks.lop.Map
will be useful for long-running callbacks, such as i/o bound processing.for
beats other implementations for memory and CPU.
- Ping me on Twitter@samuelberthe (DMs, mentions, whatever :))
- Fork theproject
- Fixopen issues or request new features
Don't hesitate ;)
Helper naming: helpers must be self-explanatory and respect standards (other languages, libraries...). Feel free to suggest many names in your contributions.
docker-compose run --rm dev
# Install some dev dependenciesmake tools# Run testsmaketest# ormake watch-test
Give a ⭐️ if this project helped you!
Copyright © 2022Samuel Berthe.
This project is underMIT license.
About
💥 A Lodash-style Go library based on Go 1.18+ Generics (map, filter, contains, find...)