@@ -24,7 +24,7 @@ In the future, 5 to 10 helpers will overlap with those coming into the Go standa
24
24
25
25
** Why this name?**
26
26
27
- I wanted a ** short name** , similar to "Lodash" and no Go package currently uses this name.
27
+ I wanted a ** short name** , similar to "Lodash" and no Go package uses this name.
28
28
29
29
![ lo] ( img/logo-full.png )
30
30
@@ -54,7 +54,7 @@ import (
54
54
Then use one of the helpers below:
55
55
56
56
``` go
57
- names := lo.Uniq [ string ] ([]string {" Samuel" , " John" , " Samuel" })
57
+ names := lo.Uniq ([]string {" Samuel" , " John" , " Samuel" })
58
58
// []string{"Samuel", "John"}
59
59
```
60
60
@@ -894,7 +894,7 @@ Returns a slice of all non-zero elements.
894
894
``` go
895
895
in := []string {" " , " foo" , " " , " bar" , " " }
896
896
897
- slice := lo.Compact [ string ] (in)
897
+ slice := lo.Compact (in)
898
898
// []string{"foo", "bar"}
899
899
```
900
900
@@ -929,7 +929,7 @@ slice := lo.IsSortedByKey([]string{"a", "bb", "ccc"}, func(s string) int {
929
929
Creates an array of the map keys.
930
930
931
931
``` go
932
- keys := lo.Keys [ string , int ] (map [string ]int {" foo" : 1 , " bar" : 2 })
932
+ keys := lo.Keys (map [string ]int {" foo" : 1 , " bar" : 2 })
933
933
// []string{"foo", "bar"}
934
934
```
935
935
@@ -951,10 +951,10 @@ values := lo.Values[string, int](map[string]int{"foo": 1, "bar": 2})
951
951
Returns the value of the given key or the fallback value if the key is not present.
952
952
953
953
``` go
954
- value := lo.ValueOr [ string , int ] (map [string ]int {" foo" : 1 , " bar" : 2 }, " foo" , 42 )
954
+ value := lo.ValueOr (map [string ]int {" foo" : 1 , " bar" : 2 }, " foo" , 42 )
955
955
// 1
956
956
957
- value := lo.ValueOr [ string , int ] (map [string ]int {" foo" : 1 , " bar" : 2 }, " baz" , 42 )
957
+ value := lo.ValueOr (map [string ]int {" foo" : 1 , " bar" : 2 }, " baz" , 42 )
958
958
// 42
959
959
```
960
960
@@ -1089,7 +1089,7 @@ m2 := lo.Invert(map[string]int{"a": 1, "b": 2, "c": 1})
1089
1089
Merges multiple maps from left to right.
1090
1090
1091
1091
``` go
1092
- mergedMaps := lo.Assign [ string , int ] (
1092
+ mergedMaps := lo.Assign (
1093
1093
map [string ]int {" a" : 1 , " b" : 2 },
1094
1094
map [string ]int {" b" : 3 , " c" : 4 },
1095
1095
)
@@ -1344,6 +1344,19 @@ tuples := lo.Zip2([]string{"a", "b"}, []int{1, 2})
1344
1344
1345
1345
[[ play] ( https://go.dev/play/p/jujaA6GaJTp )]
1346
1346
1347
+ ### ZipBy2 -> ZipBy9
1348
+
1349
+ 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.
1350
+
1351
+ When collections have different size, the Tuple attributes are filled with zero value.
1352
+
1353
+ ``` go
1354
+ items := lo.ZipBy2 ([]string {" a" , " b" }, []int {1 , 2 }, func (a string , b int ) string {
1355
+ return fmt.Sprintf (" %s -%d " , a, b)
1356
+ })
1357
+ // []string{"a-1", "b-2"}
1358
+ ```
1359
+
1347
1360
### Unzip2 -> Unzip9
1348
1361
1349
1362
Unzip accepts an array of grouped elements and creates an array regrouping the elements to their pre-zip configuration.
@@ -1356,6 +1369,18 @@ a, b := lo.Unzip2([]Tuple2[string, int]{{A: "a", B: 1}, {A: "b", B: 2}})
1356
1369
1357
1370
[[ play] ( https://go.dev/play/p/ciHugugvaAW )]
1358
1371
1372
+ ### UnzipBy2 -> UnzipBy9
1373
+
1374
+ UnzipBy2 iterates over a collection and creates an array regrouping the elements to their pre-zip configuration.
1375
+
1376
+ ``` go
1377
+ a , b := lo.UnzipBy2 ([]string {" hello" , " john" , " doe" }, func (str string ) (string , int ) {
1378
+ return str, len (str)
1379
+ })
1380
+ // []string{"hello", "john", "doe"}
1381
+ // []int{5, 4, 3}
1382
+ ```
1383
+
1359
1384
### ChannelDispatcher
1360
1385
1361
1386
Distributes messages from input channels into N child channels. Close events are propagated to children.
@@ -1924,6 +1949,9 @@ min := lo.Min([]int{1, 2, 3})
1924
1949
1925
1950
min := lo.Min ([]int {})
1926
1951
// 0
1952
+
1953
+ min := lo.Min ([]time.Duration {time.Second , time.Hour })
1954
+ // 1s
1927
1955
```
1928
1956
1929
1957
### MinBy
@@ -1958,6 +1986,9 @@ max := lo.Max([]int{1, 2, 3})
1958
1986
1959
1987
max := lo.Max ([]int {})
1960
1988
// 0
1989
+
1990
+ max := lo.Max ([]time.Duration {time.Second , time.Hour })
1991
+ // 1h
1961
1992
```
1962
1993
1963
1994
### MaxBy
@@ -2712,7 +2743,7 @@ lo.Must0(ok, "'%s' must always contain '%s'", myString, requiredChar)
2712
2743
2713
2744
list := []int {0 , 1 , 2 }
2714
2745
item := 5
2715
- lo.Must0 (lo.Contains [ int ] (list, item), " '%s ' must always contain '%s '" , list, item)
2746
+ lo.Must0 (lo.Contains (list, item), " '%s ' must always contain '%s '" , list, item)
2716
2747
...
2717
2748
```
2718
2749
@@ -2949,7 +2980,7 @@ make watch-test
2949
2980
2950
2981
Give a ⭐️ if this project helped you!
2951
2982
2952
- [ ![ support us ] ( https://c5.patreon.com/external/logo/become_a_patron_button.png )] ( https://www.patreon. com/samber )
2983
+ [ ![ GitHub Sponsors ] ( https://img.shields.io/github/sponsors/samber?style=for-the-badge )] ( https://github. com/sponsors /samber )
2953
2984
2954
2985
## 📝 License
2955
2986
0 commit comments