feat: 深度克隆支持map复制

This commit is contained in:
2025-11-27 14:50:27 +08:00
parent b0d0a6db28
commit 30c1d54f49
4 changed files with 72 additions and 0 deletions

View File

@ -8,6 +8,7 @@
package op_array
import (
"fmt"
"testing"
"git.zhangdeman.cn/zhangdeman/serialize"
@ -32,3 +33,38 @@ func TestTree(t *testing.T) {
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)
}