多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
~~~ package toolutilv3 import ( "fmt" "reflect" "testing" ) // AppendHead 向切片头部追加元素 func TestAppendHead(t *testing.T) { slice := []interface{}{1.23, 2, 3.45, 4.56} result := ToolSlice.AppendHead(slice, 1.1) fmt.Printf("len: %d, cap: %d \r\n", len(result), cap(result)) fmt.Println(result) } // ChangeInterfaceToSlice 将 []interface{} 类型的切片转换成指定类型的切片 func TestChangeInterfaceToSlice(t *testing.T) { slice := []interface{}{1.23, 2, 3.45, 4.56} elementType := reflect.TypeOf(float64(0)) result := ToolSlice.ChangeInterfaceToSlice(slice, elementType) fmt.Println(result) } // ChangeSliceToInterface 将任意切片类型的切片 转换为 []interface{} 类型 func TestChangeSliceToInterface(t *testing.T) { slice := []float64{1.23, 2, 3.45, 4.56} result := ToolSlice.ChangeSliceToInterface(slice) fmt.Println(result) } // Contains 方法可以检查给定的切片中是否存在某个元素 func TestContains(t *testing.T) { result := ToolSlice.Contains([]int{1, 2, 3}, 1) fmt.Println(result) } // ConvertSliceToMap 将结构体切片转出[]map[string]interface{} func TestConvertSliceToMap(t *testing.T) { type Data struct { Field1 int Field2 string } data := make([]Data, 0, 3) data = append(data, Data{1, "A"}) data = append(data, Data{2, "B"}) data = append(data, Data{3, "C"}) result := ToolSlice.ConvertSliceToMap(data) fmt.Println(result) } // FindDiffVales 找到两个切片中不同的元素,返回两个切片中不同元素组成的切片 func TestFindDiffVales(t *testing.T) { slice1 := []int{1, 3, 5, 7, 9} slice2 := []int{2, 3, 4, 5, 6} result := ToolSlice.FindDiffVales( ToolSlice.ChangeSliceToInterface(slice1), ToolSlice.ChangeSliceToInterface(slice2), ) fmt.Println(result) } // FindSameVales 找到两个切片中同同的元素,返回两个切片中相同同元素组成的切片 func TestFindSameVales(t *testing.T) { slice1 := []int{1, 3, 5, 7, 9} slice2 := []int{2, 3, 4, 5, 6} result := ToolSlice.FindSameVales( ToolSlice.ChangeSliceToInterface(slice1), ToolSlice.ChangeSliceToInterface(slice2), ) fmt.Println(result) } // FindOutVales 从slice1里剔除slice2存在的值 func TestFindOutVales(t *testing.T) { slice1 := []int{1, 3, 5, 7, 9} slice2 := []int{2, 3, 4, 5, 6} result := ToolSlice.FindOutVales( ToolSlice.ChangeSliceToInterface(slice1), ToolSlice.ChangeSliceToInterface(slice2), ) fmt.Println(result) } // GroupByKey 将[]map[string]interface{}按某个字段分组 func TestGroupByKey(t *testing.T) { data := []map[string]interface{}{ {"name": "Alice", "age": 19, "gender": "female"}, {"name": "Bob", "age": 22, "gender": "male"}, {"name": "Charlie", "age": 25, "gender": "male"}, {"name": "David", "age": 21, "gender": "male"}, {"name": "Eve", "age": 20, "gender": "female"}, } result := ToolSlice.GroupByKey(data, "gender", "people") fmt.Println(result) } // MapListKeyToIntSlice 函数从输入的 []map[string]interface{} 类型的切片中提取指定 key 对应的 int 类型数值,并返回一个 []int 类型的切片。 func TestMapListKeyToIntSlice(t *testing.T) { data := []map[string]interface{}{ {"name": "Alice", "age": 19}, {"name": "Bob", "age": 22}, {"name": "Charlie", "age": 25}, {"name": "David", "age": 21}, {"name": "Eve", "age": 20}, } result := ToolSlice.MapListKeyToIntSlice(data, "age") fmt.Println(result) } // UniqueInterface 函数用于合并多个任意类型的切片并去重 func TestUniqueInterface(t *testing.T) { slice1 := ToolSlice.ChangeSliceToInterface([]int{1, 3, 3, 5, 7, 9}) result1 := ToolSlice.UniqueInterface(slice1) slice2 := ToolSlice.ChangeSliceToInterface([]int{2, 4, 5}) result2 := ToolSlice.UniqueInterface(slice1, slice2) fmt.Println(result1, result2) } // SetNilToDefaultValueByMap 字典中所有键名在 keys 数组中出现过且对应的键值为 nil 的元素改为默认值 defaultValue func TestSetNilToDefaultValueByMap(t *testing.T) { m := map[string]interface{}{ "name": "张三", "age": nil, "gender": "male", } ToolSlice.SetNilToDefaultValueByMap(m, []string{"age"}, "[]") fmt.Println(m) } // SetNilToDefaultValueByMapV2 处理nil值,出现在keys的nil设置成[], 其余的nil设置成"" func TestSetNilToDefaultValueByMapV2(t *testing.T) { m := map[string]interface{}{ "name": "张三", "age1": nil, "age2": nil, "gender": "male", } ToolSlice.SetNilToDefaultValueByMapV2(m, []string{"age1"}) fmt.Println(m) } // SetNilToDefaultValueBySlice 切片中所有键名在 keys 数组中出现过且对应的键值为 nil 的元素改为默认值 defaultValue func TestSetNilToDefaultValueBySlice(t *testing.T) { slice := []map[string]interface{}{ {"name": "张三", "age": 20, "gender": "male"}, {"name": "李四", "age": nil, "gender": "female"}, {"name": "王五", "age": 22, "gender": nil}, } ToolSlice.SetNilToDefaultValueBySlice(slice, []string{"age", "gender"}, "") fmt.Println(slice) } // SetNilToDefaultValueBySliceV2 处理nil值,出现在keys的nil设置成[], 其余的nil设置成"" func TestSetNilToDefaultValueBySliceV2(t *testing.T) { slice := []map[string]interface{}{ {"name": "张三", "age": 20, "other": []interface{}{"一", "二"}}, {"name": nil, "age": 19, "other": []interface{}{"一", "二"}}, {"name": nil, "age": 22, "other": nil}, } ToolSlice.SetNilToDefaultValueBySliceV2(slice, []string{"other"}) fmt.Println(slice) } // SortByKey 函数按照指定的 key 对应的值,对输入的 []map[string]interface{} 类型的切片进行排序。 func TestSortByKey(t *testing.T) { data := []map[string]interface{}{ {"name": "Alice", "pp": 10.001}, {"name": "Bob", "pp": 55.004}, {"name": "Charlie", "pp": 10.004}, {"name": "David", "pp": 55.003}, {"name": "Eve", "pp": 55.002}, } ToolSlice.SortByKey(data, "pp", true) fmt.Println(data) ToolSlice.SortByKey(data, "pp", false) fmt.Println(data) } // StringToSlice 将字符串解析为切片类型 func TestStringToSlice(t *testing.T) { data := "[\"张三\",\"李四\",\"王五\",\"赵六\"]" result := ToolSlice.StringToSlice(data, reflect.TypeOf("")) fmt.Println(result) } // StringToSliceInt 将字符串解析为Int切片类型 func TestStringToSliceInt(t *testing.T) { data := "[9,5,3,7]" result := ToolSlice.StringToSliceInt(data) fmt.Println(result) } // StringToMap 将字符串解析为字典类型 func TestStringToMap(t *testing.T) { data := "{\"name\":\"张三\",\"age\":18}" result := ToolSlice.StringToMap(data) fmt.Println(result) } // StringToMapSlice 将字符串解析为切片类型 func TestStringToMapSlice(t *testing.T) { data := "[{\"name\":\"张三\",\"age\":18},{\"name\":\"李四\",\"age\":19},{\"name\":\"王五\",\"age\":20}]" result := ToolSlice.StringToMapSlice(data) fmt.Println(result) } ~~~