go中的深度拷贝方式
1.使用json的序列化和反序列化
通过将原来的结构序列化成byte数组,然后将byte数组反序列化到
目标结构的方式来进行深度拷贝。相关代码如下:
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
|
package main import ( "encoding/json" "fmt" "reflect" ) type ( Player struct { Id int Level int Heroes map [int]*Hero Equips []*Equip } Hero struct { Id int Level int Skills []*Skill } Equip struct { Id int Level int } Skill struct { Id int Level int } ) func NewHero() *Hero { return &Hero{ Id: 1, Level: 1, Skills: append([]*Skill{NewSkill()}, NewSkill(), NewSkill()), } } func NewSkill() *Skill { return &Skill{1, 1} } func NewEquip() *Equip { return &Equip{1, 1} } func NewPlayer() *Player { return &Player{ Id: 1, Level: 1, Heroes: map [int]*Hero{1: NewHero(), 2: NewHero(), 3: NewHero()}, Equips: append([]*Equip{NewEquip()}, NewEquip(), NewEquip()), } } func CopyByJson(dst interface {}, src interface {}) error { if dst == nil { return fmt.Errorf( "dst cannot be nil" ) } if src == nil { return fmt.Errorf( "src cannot be nil" ) } bytes, err := json.Marshal(src) if err != nil { return fmt.Errorf( "Unable to marshal src: %s" , err) } err = json.Unmarshal(bytes, dst) if err != nil { return fmt.Errorf( "Unable to unmarshal into dst: %s" , err) } return nil } func (self *Hero) Print() { fmt.Printf( "Id=%d, Level=%d
" , self.Id, self.Level) for _, v := range self.Skills { fmt.Printf( "%v
" , *v) } } func (self *Player) Print() { fmt.Printf( "Id=%d, Level=%d
" , self.Id, self.Level) for _, v := range self.Heroes { v.Print() } for _, v := range self.Equips { fmt.Printf( "%+v
" , *v) } } func main() { p1 := NewPlayer() p2 := new(Player) CopyByJson(p2, p1) fmt.Println(reflect.DeepEqual(p1, p2)) p2.Print() } output: true //对应的benchmark func BenchmarkCopyByJson(b *testing.B) { p1 := NewPlayer() p2 := new(Player) for i:=0 ; i<b.N ; i++ { CopyByJson(p2, p1) } } goos: windows goarch: amd64 pkg: game.lab/ go -deepcopy/src/main 100000 21600 ns/op PASS |