使用例子 包含了以下几个主要部分基本切片操作Filter- 过滤切片元素Map- 转换切片元素Reduce- 归约切片元素ForEach- 遍历切片元素切片转换操作SliceToMap- 将切片转换为映射KeyBy- 按键分组FilterMap- 过滤并转换FlatMap- 扁平化转换切片处理操作Uniq- 去重UniqBy- 按条件去重GroupBy- 分组Chunk- 分块PartitionBy- 分区切片修改操作Drop- 从开头删除元素DropRight- 从结尾删除元素Take- 取前几个元素TakeWhile- 条件取元素Replace- 替换元素ReplaceAll- 替换所有元素其他实用功能Count- 计数CountBy- 按条件计数CountValues- 计数值Compact- 移除零值IsSorted- 检查是否排序IsSortedBy- 按条件检查是否排序Subset- 取子集Splice- 插入元素高级切片操作Concat- 连接切片Window- 滑动窗口Sliding- 自定义滑动窗口Interleave- 交错切片Trim- 修剪切片Cut- 切割切片每个测试函数都包含了详细的注释和使用示例展示了如何使用这些函数来处理切片数据。您可以通过运行这个测试文件来查看各种操作的输出结果从而更好地理解这些函数的用法。package webtestimport (fmttestinggithub.com/samber/logithub.com/stretchr/testify/suite)// TestSliceSuite 测试 lo 包中的切片操作type TestSliceSuite struct {suite.Suite}func (self *TestSliceSuite) SetupSuite() {}func (self *TestSliceSuite) SetupTest() {}func TestSliceSuites(t *testing.T) {suite.Run(t, new(TestSliceSuite))}// 基本切片操作func (self *TestSliceSuite) Test001_BasicSliceOperations() {// 准备测试数据numbers : []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}users : []struct {ID intName stringAge int}{{1, Alice, 25},{2, Bob, 30},{3, Charlie, 20},{4, David, 35},{5, Eve, 28},}// Filter - 过滤切片元素evenNumbers : lo.Filter(numbers, func(item int, index int) bool {return item%2 0})fmt.Println(Even numbers:, evenNumbers) // [2 4 6 8 10]// Map - 转换切片元素names : lo.Map(users, func(item struct {ID intName stringAge int}, index int) string {return item.Name})fmt.Println(Names:, names) // [Alice Bob Charlie David Eve]// Reduce - 归约切片元素sum : lo.Reduce(numbers, func(agg int, item int, index int) int {return agg item}, 0)fmt.Println(Sum:, sum) // 55// ForEach - 遍历切片元素fmt.Println(ForEach:)lo.ForEach(users, func(item struct {ID intName stringAge int}, index int) {fmt.Printf(User %d: %s, %d years old\n, item.ID, item.Name, item.Age)})}// 切片转换操作func (self *TestSliceSuite) Test002_SliceTransformations() {// 准备测试数据users : []struct {ID intName stringAge int}{{1, Alice, 25},{2, Bob, 30},{3, Charlie, 20},{4, David, 35},{5, Eve, 28},}// SliceToMap - 将切片转换为映射userMap : lo.SliceToMap(users, func(item struct {ID intName stringAge int}) (int, string) {return item.ID, item.Name})fmt.Println(User map:, userMap) // map[1:Alice 2:Bob 3:Charlie 4:David 5:Eve]// KeyBy - 按键分组userByAge : lo.KeyBy(users, func(item struct {ID intName stringAge int}) int {return item.Age})fmt.Println(User by age:, userByAge)// FilterMap - 过滤并转换adultNames : lo.FilterMap(users, func(item struct {ID intName stringAge int}, index int) (string, bool) {return item.Name, item.Age 25})fmt.Println(Adult names:, adultNames) // [Alice Bob David Eve]// FlatMap - 扁平化转换nested : [][]int{{1, 2}, {3, 4}, {5, 6}}flattened : lo.FlatMap(nested, func(item []int, index int) []int {return item})fmt.Println(Flattened:, flattened) // [1 2 3 4 5 6]}// 切片处理操作func (self *TestSliceSuite) Test003_SliceProcessing() {// 准备测试数据numbers : []int{1, 2, 2, 3, 3, 3, 4, 4, 4, 4}users : []struct {ID intName stringAge int}{{1, Alice, 25},{2, Bob, 30},{3, Charlie, 20},{4, David, 35},{5, Eve, 25},}// Uniq - 去重uniqueNumbers : lo.Uniq(numbers)fmt.Println(Unique numbers:, uniqueNumbers) // [1 2 3 4]// UniqBy - 按条件去重uniqueByAge : lo.UniqBy(users, func(item struct {ID intName stringAge int}) int {return item.Age})fmt.Println(Unique by age:, uniqueByAge)// GroupBy - 分组groupedByAge : lo.GroupBy(users, func(item struct {ID intName stringAge int}) int {return item.Age})fmt.Println(Grouped by age:, groupedByAge)// Chunk - 分块chunks : lo.Chunk(numbers, 3)fmt.Println(Chunks:, chunks) // [[1 2 2] [3 3 3] [4 4 4] [4]]// PartitionBy - 分区partitionedByAge : lo.PartitionBy(users, func(item struct {ID intName stringAge int}) string {if item.Age 25 {return young} else if item.Age 30 {return adult} else {return senior}})fmt.Println(Partitioned by age:, partitionedByAge)}// 切片修改操作func (self *TestSliceSuite) Test004_SliceModifications() {// 准备测试数据numbers : []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}// Drop - 从开头删除元素dropped : lo.Drop(numbers, 3)fmt.Println(Dropped first 3:, dropped) // [4 5 6 7 8 9 10]// DropRight - 从结尾删除元素droppedRight : lo.DropRight(numbers, 3)fmt.Println(Dropped last 3:, droppedRight) // [1 2 3 4 5 6 7]// Take - 取前几个元素taken : lo.Take(numbers, 5)fmt.Println(Taken first 5:, taken) // [1 2 3 4 5]// TakeWhile - 条件取元素takenWhile : lo.TakeWhile(numbers, func(item int) bool {return item 5})fmt.Println(Taken while 5:, takenWhile) // [1 2 3 4]// Replace - 替换元素replaced : lo.Replace(numbers, 5, 100, 1)fmt.Println(Replaced first 5 with 100:, replaced) // [1 2 3 4 100 6 7 8 9 10]// ReplaceAll - 替换所有元素replacedAll : lo.ReplaceAll([]int{1, 2, 2, 3, 2}, 2, 99)fmt.Println(Replaced all 2 with 99:, replacedAll) // [1 99 99 3 99]}// 其他实用功能func (self *TestSliceSuite) Test005_OtherUsefulFunctions() {// 准备测试数据numbers : []int{1, 2, 3, 4, 5, 2, 3, 4, 2}mixed : []int{0, 1, 0, 2, 0, 3, 0, 4}sorted : []int{1, 2, 3, 4, 5}unsorted : []int{3, 1, 4, 2, 5}// Count - 计数count2 : lo.Count(numbers, 2)fmt.Println(Count of 2:, count2) // 3// CountBy - 按条件计数countEven : lo.CountBy(numbers, func(item int) bool {return item%2 0})fmt.Println(Count of even numbers:, countEven) // 6// CountValues - 计数值valueCounts : lo.CountValues(numbers)fmt.Println(Value counts:, valueCounts) // map[1:1 2:3 3:2 4:2 5:1]// Compact - 移除零值compacted : lo.Compact(mixed)fmt.Println(Compacted:, compacted) // [1 2 3 4]// IsSorted - 检查是否排序isSorted : lo.IsSorted(sorted)fmt.Println(Is sorted:, isSorted) // trueisUnsorted : lo.IsSorted(unsorted)fmt.Println(Is unsorted:, isUnsorted) // false// IsSortedBy - 按条件检查是否排序users : []struct {ID intName stringAge int}{{1, Alice, 20},{2, Bob, 25},{3, Charlie, 30},}isSortedByAge : lo.IsSortedBy(users, func(item struct {ID intName stringAge int}) int {return item.Age})fmt.Println(Is sorted by age:, isSortedByAge) // true// Subset - 取子集subset : lo.Subset(numbers, 2, 4)fmt.Println(Subset from index 2, length 4:, subset) // [3 4 5 2]// Splice - 插入元素spliced : lo.Splice(numbers, 2, 100, 200)fmt.Println(Spliced at index 2:, spliced)}// 高级切片操作func (self *TestSliceSuite) Test006_AdvancedSliceOperations() {// 准备测试数据numbers1 : []int{1, 2, 3}numbers2 : []int{4, 5, 6}numbers3 : []int{7, 8, 9}// Concat - 连接切片concatenated : lo.Concat(numbers1, numbers2, numbers3)fmt.Println(Concatenated:, concatenated) // [1 2 3 4 5 6 7 8 9]// Window - 滑动窗口windowed : lo.Window(concatenated, 3)fmt.Println(Window size 3:, windowed) // [[1 2 3] [2 3 4] [3 4 5] [4 5 6] [5 6 7] [6 7 8] [7 8 9]]// Sliding - 自定义滑动窗口sliding : lo.Sliding(concatenated, 3, 2)fmt.Println(Sliding window size 3, step 2:, sliding) // [[1 2 3] [3 4 5] [5 6 7] [7 8 9]]// Interleave - 交错切片interleaved : lo.Interleave(numbers1, numbers2, numbers3)fmt.Println(Interleaved:, interleaved) // [1 4 7 2 5 8 3 6 9]// Trim - 修剪切片toTrim : []int{0, 0, 1, 2, 3, 0, 0}trimmed : lo.Trim(toTrim, []int{0})fmt.Println(Trimmed:, trimmed) // [1 2 3]// Cut - 切割切片toCut : []int{1, 2, 3, 4, 5, 6}before, after, found : lo.Cut(toCut, []int{3, 4})fmt.Println(Cut before:, before) // [1 2]fmt.Println(Cut after:, after) // [5 6]fmt.Println(Cut found:, found) // true}