-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
131 lines (117 loc) · 2.98 KB
/
Copy pathexample_test.go
File metadata and controls
131 lines (117 loc) · 2.98 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package oper_test
import (
"fmt"
"strings"
"oper"
)
// ExampleIn 演示 oper.In 元素包含检查。
func ExampleIn() {
list := []int{1, 2, 3, 4, 5}
fmt.Println(oper.In(list, 3))
fmt.Println(oper.In(list, 99))
// Output:
// true
// false
}
// ExampleFilter 演示 oper.Filter 过滤函数。
func ExampleFilter() {
evens := oper.Filter([]int{1, 2, 3, 4, 5, 6}, func(n int) bool {
return n%2 == 0
})
fmt.Println(evens)
// Output: [2 4 6]
}
// ExampleMap 演示 oper.Map 映射函数。
func ExampleMap() {
doubled := oper.Map([]int{1, 2, 3}, func(n int) int {
return n * 2
})
fmt.Println(doubled)
// Output: [2 4 6]
}
// ExampleChunk 演示 oper.Chunk 分块。
func ExampleChunk() {
chunks := oper.Chunk([]int{1, 2, 3, 4, 5, 6, 7}, 3)
fmt.Println(chunks)
// Output: [[1 2 3] [4 5 6] [7]]
}
// ExampleGroupBy 演示 oper.GroupBy 分组。
func ExampleGroupBy() {
// 用 role 字符串作为 key 分组
roles := []string{"admin", "user", "admin", "user", "guest"}
byRole := oper.GroupBy(roles, func(r string) string { return r })
fmt.Println("admin:", byRole["admin"])
fmt.Println("user:", byRole["user"])
// Output:
// admin: [admin admin]
// user: [user user]
}
// ExampleChunk_realtime 演示按 size 分块。
func ExampleChunk_size() {
for _, batch := range oper.Chunk([]int{1, 2, 3, 4, 5, 6, 7}, 3) {
fmt.Println(batch)
}
// Output:
// [1 2 3]
// [4 5 6]
// [7]
}
// ExampleUniqBy 演示按 key 去重。
func ExampleUniqBy() {
// 演示用整型切片(List 约束只支持基础类型)
values := oper.UniqBy([]int{1, 2, 2, 3, 3, 3, 4}, func(n int) int { return n })
fmt.Println(values)
// Output: [1 2 3 4]
}
// ExampleSum 演示求和。
func ExampleSum() {
total := oper.Sum([]int{1, 2, 3, 4, 5})
fmt.Println(total)
// Output: 15
}
// ExampleCamelCase 演示字符串转驼峰。
func ExampleCamelCase() {
fmt.Println(oper.CamelCase("hello_world"))
fmt.Println(oper.CamelCase("user-profile"))
// Output:
// helloWorld
// userProfile
}
// ExampleGet 演示从 map 嵌套取值。
func ExampleGet() {
data := map[string]interface{}{
"user": map[string]interface{}{
"name": "alice",
"addr": map[string]interface{}{
"city": "Xiamen",
},
},
}
city := oper.Get(data, "user.addr.city", "unknown")
fmt.Println(city)
// Output: Xiamen
}
// ExampleIn_string 演示字符串切片包含检查。
func ExampleIn_string() {
roles := []string{"admin", "user", "guest"}
fmt.Println(oper.In(roles, "admin"))
fmt.Println(oper.In(roles, "root"))
// Output:
// true
// false
}
// ExampleChunk_strings 演示字符串分块(用于批量处理)。
func ExampleChunk_strings() {
urls := []string{
"https://a.com/1", "https://a.com/2", "https://a.com/3",
"https://a.com/4", "https://a.com/5",
}
batches := oper.Chunk(urls, 2)
for i, batch := range batches {
fmt.Printf("batch %d: %s\n", i, strings.Join(batch, ", "))
}
// Output:
// batch 0: https://a.com/1, https://a.com/2
// batch 1: https://a.com/3, https://a.com/4
// batch 2: https://a.com/5
}