Files
wrapper/op_array/util_test.go

71 lines
1.7 KiB
Go

// Package op_array ...
//
// Description : op_array ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2025-11-27 12:40
package op_array
import (
"fmt"
"testing"
"git.zhangdeman.cn/zhangdeman/serialize"
)
func TestTree(t *testing.T) {
type Data struct {
ID int `json:"id" dc:"数据ID"`
ParentID int `json:"parent_id" dc:"父级ID"`
Value string `json:"value" dc:"数据值"`
}
dataList := []Data{
{ID: 1, ParentID: 0, Value: "1"},
{ID: 2, ParentID: 1, Value: "2"},
{ID: 3, ParentID: 1, Value: "3"},
{ID: 4, ParentID: 2, Value: "4"},
{ID: 5, ParentID: 2, Value: "5"},
{ID: 6, ParentID: 3, Value: "6"},
{ID: 7, ParentID: 3, Value: "7"},
}
res := Tree(dataList, func(v Data) int { return v.ParentID }, func(v Data) int { return v.ID })
serialize.JSON.ConsoleOutput(res)
}
func TestClone(t *testing.T) {
type Data struct {
ID int `json:"id" dc:"数据ID"`
}
// 字面结构体
dataList := []Data{
{ID: 1},
{ID: 2},
}
res := DeepClone(dataList)
fmt.Println(fmt.Sprintf("%p", &res[0]), fmt.Sprintf("%p", &dataList[0]))
fmt.Println(fmt.Sprintf("%p", &res[1]), fmt.Sprintf("%p", &dataList[1]))
// 结构体指针
dataList1 := []*Data{
{ID: 1},
{ID: 2},
}
res1 := DeepClone(dataList1)
fmt.Println(fmt.Sprintf("%p", &res1[0]), fmt.Sprintf("%p", &dataList1[0]))
fmt.Println(fmt.Sprintf("%p", &res1[1]), fmt.Sprintf("%p", &dataList1[1]))
// Map数据
dataList2 := []map[string]any{
{"id": 1},
{"id": 2},
}
res2 := DeepClone(dataList2)
fmt.Println(fmt.Sprintf("%p", &res2[0]), fmt.Sprintf("%p", &dataList2[0]))
fmt.Println(fmt.Sprintf("%p", &res2[1]), fmt.Sprintf("%p", &dataList2[1]))
serialize.JSON.ConsoleOutput(res2)
serialize.JSON.ConsoleOutput(dataList2)
}