22.lo
# 01.切片
- https://zhuanlan.zhihu.com/p/569464541
# 1、Filter
- 这个函数实现了Python中的 filter函数:迭代一个slice,计算每个元素,如果匿名函数返回false,则被过滤掉。
package main
import (
"fmt"
"github.com/samber/lo"
"strings"
)
func main() {
strs := lo.Filter[string]([]string{"hello", "good bye", "world", "fuck", "fuck who"}, func(s string, _ int) bool {
return !strings.Contains(s, "fuck")
})
fmt.Println(strs) //[hello good bye world]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# 2、Map
- 这个函数将slice中每个元素映射成另一个元素,新元素的类型不一定和源元素的相同。
package main
import (
"fmt"
"github.com/samber/lo"
"strconv"
)
func main() {
intNums := lo.Map[string, int]([]string{"1", "2", "3"}, func(s string, _ int) int {
num, _ := strconv.Atoi(s)
return num
})
fmt.Printf("%T %v", intNums, intNums) // []int [1 2 3]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 3、FilterMap
- 这个函数对slice同时执行filter和map,分别调用 filter 和 map也能实现这个功能
- 为什么还要实现
FilterMap
呢?原因也很简单,FilterMap
可以减少一个循环
package main
import (
"fmt"
"github.com/samber/lo"
"strings"
)
func main() {
m := []string{"cpu", "gpu", "mouse", "keyboard"}
matching := lo.FilterMap[string, string](m, func(x string, _ int) (string, bool) {
if strings.HasSuffix(x, "pu") {
return "xpu", true
}
return "", false
})
fmt.Println(matching) // []string{"xpu", "xpu"}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 4、FlatMap
- 这个函数将slice中每个元素转换成另一个slice,最后合并成一个slice
package main
import (
"fmt"
"github.com/samber/lo"
)
func main() {
flatMap := lo.FlatMap[int64, string]([]int64{1, 2, 3}, func(i int64, _ int) []string {
return []string{
fmt.Sprintf("%v", i),
fmt.Sprintf("%v", i*10),
}
})
fmt.Println(flatMap) //[1 10 2 20 3 30]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 5、Reduce
- 这个函数和Python中的reduce一样,将一个slice经过计算,变成一个标量值。
package main
import (
"fmt"
"github.com/samber/lo"
)
func main() {
sum := lo.Reduce[int, int]([]int{1, 2, 3, 4}, func(agg int, item int, _ int) int {
return agg + item
}, 0)
fmt.Println(sum) // 10(1 + 2 + 3 + 4)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# 6、ReduceRight
- 和 reduce一样,只不过从右向左迭代
package main
import (
"fmt"
"github.com/samber/lo"
)
func main() {
v := [][]int{{0, 1}, {2, 3}, {4, 5}}
result := lo.ReduceRight[[]int, []int](v, func(agg []int, item []int, _ int) []int {
return append(agg, item...)
}, []int{})
fmt.Println(result) // []int{4, 5, 2, 3, 0, 1}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# 7、ForEach
package main
import (
"github.com/samber/lo"
)
func main() {
lo.ForEach[string]([]string{"hello", "world"}, func(x string, _ int) {
println(x)
})
//hello
//world
}
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# 8、Times
- 调用指定函数 n 次,将每次的返回值合并成一个slice。
package main
import (
"fmt"
"github.com/samber/lo"
"strconv"
)
func main() {
result := lo.Times[string](3, func(i int) string {
return strconv.FormatInt(int64(i), 10)
})
fmt.Printf("%T %v", result, result) // []string [0 1 2]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# 9、Uniq
package main
import (
"fmt"
"github.com/samber/lo"
)
func main() {
names := lo.Uniq[string]([]string{"Samuel", "John", "Samuel"})
fmt.Println(len(names)) // 2
fmt.Println(names) // [Samuel John]
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
# 10、UniqBy
- 去重的同时,对元素执行一些计算。
package main
import (
"fmt"
"github.com/samber/lo"
)
func main() {
uniqValues := lo.UniqBy[int, int]([]int{1, 2, 3, 5}, func(i int) int {
return i % 4
})
fmt.Println(uniqValues) // [1 2 3]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# 11、GroupBy
- 对slice元素分组,返回一个map,key是分组,value是属于这个分组的元素。
package main
import (
"fmt"
"github.com/samber/lo"
)
func main() {
group := lo.GroupBy[int, int]([]int{1, 2, 3, 4, 5, 6, 7, 8}, func(i int) int {
return i % 2
})
fmt.Println(group) // map[0:[2 4 6 8] 1:[1 3 5 7]]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# 12、Chunk
- 将slice分块
package main
import (
"fmt"
"github.com/samber/lo"
)
func main() {
result := lo.Chunk[int]([]int{0, 1, 2, 3, 4, 5, 6}, 2)
fmt.Println(result) // [[0 1] [2 3] [4 5] [6]]
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 13、PartitionBy[并行]
- 和groupBy非常像,但返回slice
package main
import (
"fmt"
"github.com/samber/lo"
)
func main() {
partitions := lo.PartitionBy[int, string]([]int{-2, -1, 0, 1, 2, 3, 4, 5}, func(x int) string {
if x < 0 {
return "负数"
} else if x%2 == 0 {
return "偶数"
}
return "其他"
})
fmt.Println(partitions) // [[-2 -1] [0 2 4] [1 3 5]]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 14、Flatten
- 将多层级的slice变成一层slice
package main
import (
"fmt"
"github.com/samber/lo"
)
func main() {
flat := lo.Flatten[int]([][]int{{0, 1}, {2, 3, 4, 5}})
fmt.Println(flat) // [0 1 2 3 4 5]
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 15、Shuffle
- 使用 Fisher-Yates 算法洗牌
randomOrder := lo.Shuffle[int]([]int{0, 1, 2, 3, 4, 5})
// []int{1, 4, 0, 3, 5, 2}
1
2
2
# 16、Reverse
反转顺序
reverseOrder := lo.Reverse[int]([]int{0, 1, 2, 3, 4, 5})
// []int{5, 4, 3, 2, 1, 0}
1
2
2
# 17、FillDrop
- 丢掉前 n 个元素
package main
import (
"fmt"
"github.com/samber/lo"
)
func main() {
l := lo.Drop[int]([]int{0, 1, 2, 3, 4, 5}, 2)
fmt.Println(l) // []int{2, 3, 4, 5}
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 18、DropRight
- 丢掉后 n 个元素。
l := lo.DropRight[int]([]int{0, 1, 2, 3, 4, 5}, 2)
// []int{0, 1, 2, 3}
1
2
2
# 19、dropWhile
- 从前往后,丢掉不符合回调函数要求的元素,直到某个元素不满足要求时停止,返回剩下的元素
- 剩下的元素不论满足不满足回调函数,都会返回
l := lo.DropWhile[string]([]string{"a", "aa", "aaa", "aa", "aa"}, func(val string) bool {
return len(val) <= 2
})
// []string{"aaa", "aa", "aa"}
1
2
3
4
2
3
4
# 20、dropRightWhile
- 和 DropWhile 类似,从后往前计算,丢掉后面几个满足要求的元素,直到某个元素不满足要求时停止,返回剩下的元素
- 剩下的元素不论满足不满足回调函数,都会返回
l := lo.DropRightWhile[string]([]string{"a", "aa", "aaa", "aa", "aa"}, func(val string) bool {
return len(val) <= 2
})
// []string{"a", "aa", "aaa"}
1
2
3
4
2
3
4
# 21、Count
和python的 list.count(x)
一样,返回某个元素出现的次数。
count := lo.Count[int]([]int{1, 5, 1}, 1)
// 2
1
2
2
# 22、CountBy
计算符合回调函数的元素的个数。
count := lo.CountBy[int]([]int{1, 5, 1}, func(i int) bool {
return i < 4
})
// 2
1
2
3
4
2
3
4
# 23、Subset
- 实现了
slice[start:start+length]
。但越界时不会panic
package main
import (
"fmt"
"github.com/samber/lo"
)
func main() {
in := []int{0, 1, 2, 3, 4}
sub1 := lo.Subset(in, 2, 3)
fmt.Println(sub1) // []int{2, 3, 4}
sub2 := lo.Subset(in, -3, 3)
fmt.Println(sub2) // [2 3 4]
sub3 := lo.Subset(in, -2, 1000)
fmt.Println(sub3) // []int{3, 4}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 24、Slice
- 实现了 slice[start:end]。但越界时不会panic。
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{}
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# 25、Replace
- 替换slice中的元素,可以指定原始值,替换后的值,以及替换次数。
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}
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# 26、ReplaceAll
- 和 Replace 一样,但不能指定替换次数。
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}
1
2
3
4
5
6
7
2
3
4
5
6
7
# 27、Compact
去除零值元素。
in := []string{"", "foo", "", "bar", ""}
slice := lo.Compact[string](in)
// []string{"foo", "bar"}
1
2
3
4
2
3
4
# 28、IsSorted
判断slcie是否有序
slice := lo.IsSorted([]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9})
// true
1
2
2
# 02.map
# 1、Keys
- 返回map的所有key的切片
keys := lo.Keys[string, int](map[string]int{"foo": 1, "bar": 2})
// []string{"bar", "foo"}
1
2
2
# 2、Values
- 返回map的所有value切片
values := lo.Values[string, int](map[string]int{"foo": 1, "bar": 2})
// []int{1, 2}
1
2
2
# 3、PickBy
- 类似于slice的filter。只返回一个map中符合条件的key/value对。
m := lo.PickBy[string, int](map[string]int{"foo": 1, "bar": 2, "baz": 3}, func(key string, value int) bool {
return value%2 == 1
})
// map[string]int{"foo": 1, "baz": 3}
1
2
3
4
2
3
4
# 4、PickByKeys
- 只返回给定key的map
package main
import (
"fmt"
"github.com/samber/lo"
)
func main() {
m := lo.PickByKeys[string, int](map[string]int{"foo": 1, "bar": 2, "baz": 3}, []string{"foo", "baz"})
fmt.Println(m)
// map[string]int{"foo": 1, "baz": 3}
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
# 5、PickByValues
- 只返回给定值的map
m := lo.PickByValues[string, int](map[string]int{"foo": 1, "bar": 2, "baz": 3}, []int{1, 3})
// map[string]int{"foo": 1, "baz": 3}
1
2
2
# 6、OmitBy
- 忽略掉符合条件的,返回剩下的map
m := lo.OmitBy[string, int](map[string]int{"foo": 1, "bar": 2, "baz": 3}, func(key string, value int) bool {
return value%2 == 1
})
// map[string]int{"bar": 2}
1
2
3
4
2
3
4
# 7、OmitByKeys
- 忽略掉给定key,剩下的key组成新map
m := lo.OmitByKeys[string, int](map[string]int{"foo": 1, "bar": 2, "baz": 3}, []string{"foo", "baz"})
// map[string]int{"bar": 2}
1
2
2
# 8、OmitByValues
- 忽略掉给定value,剩下的组成新map
m := lo.OmitByValues[string, int](map[string]int{"foo": 1, "bar": 2, "baz": 3}, []int{1, 3})
// map[string]int{"bar": 2}
1
2
2
# 9、Entries
- 将map变成key/value对 (alias: ToPairs)
package main
import (
"encoding/json"
"fmt"
"github.com/samber/lo"
)
func main() {
entries := lo.Entries[string, int](map[string]int{"foo": 1, "bar": 2})
strByte, _ := json.Marshal(entries)
fmt.Println(string(strByte)) // [{"Key":"foo","Value":1},{"Key":"bar","Value":2}]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# 10、FromEntries
- 将key/value对编程map (alias: FromPairs)
m := lo.FromEntries[string, int]([]lo.Entry[string, int]{
{
Key: "foo",
Value: 1,
},
{
Key: "bar",
Value: 2,
},
})
// map[string]int{"foo": 1, "bar": 2}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 11、Invert
- key变value,value变key。可能因为重复的value而丢失数据。
m1 := lo.Invert[string, int]([map[string]int{"a": 1, "b": 2})
// map[int]string{1: "a", 2: "b"}
m2 := lo.Invert[string, int]([map[string]int{"a": 1, "b": 2, "c": 1})
// map[int]string{1: "c", 2: "b"}
1
2
3
4
5
2
3
4
5
# 12、Assign
- 从左到右,逐个合并map,返回新map。
mergedMaps := lo.Assign[string, int](
map[string]int{"a": 1, "b": 2},
map[string]int{"b": 3, "c": 4},
)
// map[string]int{"a": 1, "b": 3, "c": 4}
1
2
3
4
5
2
3
4
5
# 13、MapKeys
- 将map的key转成另一个值。
m2 := lo.MapKeys[int, int, string](map[int]int{1: 1, 2: 2, 3: 3, 4: 4}, func(_ int, v int) string {
return strconv.FormatInt(int64(v), 10)
})
// map[string]int{"1": 1, "2": 2, "3": 3, "4": 4}
1
2
3
4
2
3
4
# 14、MapValues
- 将map的value转成另一个值
m1 := map[int]int64{1: 1, 2: 2, 3: 3}
m2 := lo.MapValues[int, int64, string](m1, func(x int64, _ int) string {
return strconv.FormatInt(x, 10)
})
// map[int]string{1: "1", 2: "2", 3: "3"}
1
2
3
4
5
6
2
3
4
5
6
# 15、MapToSlice
将key/value对转成一个值,放入slice。
m := map[int]int64{1: 4, 2: 5, 3: 6}
s := lo.MapToSlice(m, func(k int, v int64) string {
return fmt.Sprintf("%d_%d", k, v)
})
// []string{"1_4", "2_5", "3_6"}
1
2
3
4
5
6
2
3
4
5
6
# 03.string
# 1、Substring
sub := lo.Substring("hello", 2, 3) // offset=2 length=3
// "llo"
sub := lo.Substring("hello", -4, 3)
// "ell"
sub := lo.Substring("hello", -2, math.MaxUint)
// "lo"
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 2、ChunkString
- 分块
lo.ChunkString("1234567", 2)
// []string{"12", "34", "56", "7"}
1
2
2
# 3、RuneLength
package main
import (
"fmt"
"github.com/samber/lo"
)
func main() {
sub1 := lo.RuneLength("love你") // 你(长度1)
// 5
sub2 := len("love你") // 你(长度3)
// 7
fmt.Println(sub1, sub2)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 04.math
# 1、Range
result := Range(4)
// [0, 1, 2, 3]
result := Range(-4);
// [0, -1, -2, -3]
result := Range(0);
// []
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 2、RangeFrom
result := RangeFrom(1, 5);
// [1, 2, 3, 4]
result := RangeFrom[float64](1.0, 5);
// [1.0, 2.0, 3.0, 4.0]
1
2
3
4
5
2
3
4
5
# 3、RangeWithSteps
result := RangeWithSteps(0, 20, 5);
// [0, 5, 10, 15]
result := RangeWithSteps[float32](-1.0, -4.0, -1.0);
// [-1.0, -2.0, -3.0]
result := RangeWithSteps(1, 4, -1);
// []
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 4、SumBy
- 求和
strings := []string{"foo", "bar"}
sum := lo.SumBy(strings, func(item string) int {
return len(item)
})
// 6
1
2
3
4
5
2
3
4
5
# 05.tuple
# 1、Zip2
- 两个切片转map
tuples := lo.Zip2[string, int]([]string{"a", "b"}, []int{1, 2})
// []Tuple2[string, int]{{A: "a", B: 1}, {A: "b", B: 2}}
1
2
2
# 2、Unzip2
- 将字典的key和value分解成两个切片
a, b := lo.Unzip2[string, int]([]Tuple2[string, int]{{A: "a", B: 1}, {A: "b", B: 2}})
// []string{"a", "b"}
// []int{1, 2}
1
2
3
2
3
# 06.channel
# 1、ChannelDispatcher
- 将一个channel扩展出多个二级channel,然后多个consumer消费二级channel
package main
import (
"fmt"
"github.com/samber/lo"
"sync"
"time"
)
var wg sync.WaitGroup
func main() {
ch := make(chan int, 42)
go producer(ch)
children := lo.ChannelDispatcher(ch, 5, 10, lo.DispatchingStrategyRoundRobin[int])
for i := range children {
wg.Add(1)
go consumer(children[i], i)
}
// 等待所有消费者协程完成
wg.Wait()
}
func producer(ch chan int) {
for i := 0; i <= 100; i++ {
ch <- i
}
close(ch) // 关闭主通道,确保所有数据都已发送
}
func consumer(c <-chan int, i int) {
defer wg.Done()
for {
msg, ok := <-c
if !ok {
fmt.Printf("chan%v执行结束\n", i)
break
}
work(i, msg)
}
}
func work(i, msg int) {
fmt.Printf("chan(%v)获取任务%v\n", i, msg)
time.Sleep(time.Second)
}
/*
chan(4)获取任务4
chan(0)获取任务0
chan(3)获取任务3
chan(2)获取任务2
.
.
.
chan(2)获取任务96
chan(3)获取任务97
chan(0)获取任务95
chan0执行结束
chan4执行结束
chan1执行结束
chan3执行结束
chan(2)获取任务100
chan2执行结束
*/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 07.intersection
# 1、Contains
present := lo.Contains[int]([]int{0, 1, 2, 3, 4, 5}, 5)
// true
1
2
2
# 2、ContainsBy
- 集合中包含符合某种条件的元素。
present := lo.ContainsBy[int]([]int{0, 1, 2, 3, 4, 5}, func(x int) bool {
return x == 3
})
// true
1
2
3
4
2
3
4
# 3、Every
- 两个集合是否是包含关系
ok := lo.Every[int]([]int{0, 1, 2, 3, 4, 5}, []int{2, 0, 2})
// true
ok := lo.Every[int]([]int{0, 1, 2, 3, 4, 5}, []int{0, 6})
// false
1
2
3
4
5
2
3
4
5
# 4、EveryBy
- 集合元素是否都符合某种条件。
b := EveryBy[int]([]int{1, 2, 3, 4}, func(x int) bool {
return x < 5
})
// true
1
2
3
4
2
3
4
# 5、Some
- 两个集合有交集就行
ok := lo.Some[int]([]int{0, 1, 2, 3, 4, 5}, []int{0, 2})
// true
ok := lo.Some[int]([]int{0, 1, 2, 3, 4, 5}, []int{-1, 6})
// false
1
2
3
4
5
2
3
4
5
# 6、SomeBy
- 集合有任意元素满足条件就行
b := SomeBy[int]([]int{1, 2, 3, 4}, func(x int) bool {
return x < 3
})
// true
1
2
3
4
2
3
4
# 7、None
- 两个集合没有交集
b := None[int]([]int{0, 1, 2, 3, 4, 5}, []int{0, 2})
// false
b := None[int]([]int{0, 1, 2, 3, 4, 5}, []int{-1, 6})
// true
1
2
3
4
2
3
4
# 8、NoneBy
- 没有符合条件的元素就行
b := NoneBy[int]([]int{1, 2, 3, 4}, func(x int) bool {
return x < 0
})
// true
1
2
3
4
2
3
4
# 9、Intersect
- 返回两个集合的交集
result1 := lo.Intersect[int]([]int{0, 1, 2, 3, 4, 5}, []int{0, 2})
// []int{0, 2}
result2 := lo.Intersect[int]([]int{0, 1, 2, 3, 4, 5}, []int{0, 6}
// []int{0}
result3 := lo.Intersect[int]([]int{0, 1, 2, 3, 4, 5}, []int{-1, 6})
// []int{}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 10、Difference
- 会返回差集,left 为 A - B,right为 B - A
left, right := lo.Difference[int]([]int{0, 1, 2, 3, 4, 5}, []int{0, 2, 6})
// []int{1, 3, 4, 5}, []int{6}
left, right := lo.Difference[int]([]int{0, 1, 2, 3, 4, 5}, []int{0, 1, 2, 3, 4, 5})
// []int{}, []int{}
1
2
3
4
5
2
3
4
5
# 11、Union
- 并集
union := lo.Union[int]([]int{0, 1, 2, 2, 3, 4, 5}, []int{0, 2, 10})
// []int{0, 1, 2, 3, 4, 5, 10}
1
2
2
# 12、Without
- 叫Remove更合适
subset := lo.Without[int]([]int{0, 2, 10}, 2)
// []int{0, 10}
subset := lo.Without[int]([]int{0, 2, 10}, 0, 1, 2, 3, 4, 5)
// []int{10}
1
2
3
4
5
2
3
4
5
# 13、WithoutEmpty
- 移除0值
subset := lo.WithoutEmpty[int]([]int{0, 2, 10})
// []int{2, 10}
1
2
2
# 08.search
# 1、IndexOf
found := lo.IndexOf[int]([]int{0, 1, 2, 1, 2, 3}, 2)
// 2
notFound := lo.IndexOf[int]([]int{0, 1, 2, 1, 2, 3}, 6)
// -1
1
2
3
4
5
2
3
4
5
# 2、LastIndexOf
found := lo.LastIndexOf[int]([]int{0, 1, 2, 1, 2, 3}, 2)
// 4
notFound := lo.LastIndexOf[int]([]int{0, 1, 2, 1, 2, 3}, 6)
// -1
1
2
3
4
5
2
3
4
5
上次更新: 2024/3/13 15:35:10