Skip to content

Commit d3e3b7b

Browse files
committed
Update
1 parent 9c4f411 commit d3e3b7b

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

examples/advanced/deepcopy/gob_deepcopy.go

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package deepcopy
33
import (
44
"bytes"
55
"encoding/gob"
6-
"fmt"
76

87
"github.com/devlights/gomy/output"
98
)
@@ -27,17 +26,21 @@ func GobDeepCopy() error {
2726
// - encoding/json 使う
2827
//
2928
// 以下は encoding/gob を利用した方法
29+
//
30+
// gobは、Go言語で表現される型のほどんどに対応しているが、以下の型は対応していない。
31+
// - 関数型 (func)
32+
// - チャネル型(chan)
33+
// - エクスポートされていないフィールド
34+
// 上記の型の場合、定義されていても無視される。
35+
//
36+
// 基本的に、encoding/gobの方がencoding/jsonよりもパフォーマンスが良い。
3037
// --------------------------------------------------------------------------
31-
pa := func(v interface{}) string {
32-
return fmt.Sprintf("%p", v)
33-
}
34-
35-
clone := func(from, to interface{}) {
36-
buf := new(bytes.Buffer)
37-
38-
enc := gob.NewEncoder(buf)
39-
dec := gob.NewDecoder(buf)
40-
38+
clone := func(from, to any) {
39+
var (
40+
buf = new(bytes.Buffer)
41+
enc = gob.NewEncoder(buf)
42+
dec = gob.NewDecoder(buf)
43+
)
4144
_ = enc.Encode(from)
4245
_ = dec.Decode(to)
4346
}
@@ -47,15 +50,16 @@ func GobDeepCopy() error {
4750
var (
4851
i = 100
4952
i2 int
53+
5054
s = "helloworld"
5155
s2 string
5256
)
5357

5458
clone(&i, &i2)
5559
clone(&s, &s2)
5660

57-
output.Stdoutl("[i, i2]", i, i2, pa(&i), pa(&i2))
58-
output.Stdoutl("[s, s2]", s, s2, &s, &s2)
61+
output.Stdoutl("[i, i2]", i, i2)
62+
output.Stdoutl("[s, s2]", s, s2)
5963

6064
// --------------------------------------------------------------------------
6165
// スライス
@@ -65,10 +69,11 @@ func GobDeepCopy() error {
6569
)
6670

6771
clone(&sli1, &sli2)
68-
output.Stdoutl("[sli1, sli2]", sli1, sli2, pa(&sli1), pa(&sli2))
72+
output.Stdoutl("[sli1, sli2][1]", sli1, sli2)
6973

7074
sli1[0] = 100
71-
output.Stdoutl("[sli1, sli2]", sli1, sli2, pa(&sli1), pa(&sli2))
75+
sli2[1] = 200
76+
output.Stdoutl("[sli1, sli2][2]", sli1, sli2)
7277

7378
// --------------------------------------------------------------------------
7479
// マップ
@@ -78,10 +83,11 @@ func GobDeepCopy() error {
7883
)
7984

8085
clone(&map1, &map2)
81-
output.Stdoutl("[map1, map2]", map1, map2, pa(&map1), pa(&map2))
86+
output.Stdoutl("[map1, map2][1]", map1, map2)
8287

8388
map1[1] = "melon"
84-
output.Stdoutl("[map1, map2]", map1, map2, pa(&map1), pa(&map2))
89+
map2[2] = "林檎"
90+
output.Stdoutl("[map1, map2][2]", map1, map2)
8591

8692
// --------------------------------------------------------------------------
8793
// 構造体
@@ -105,11 +111,11 @@ func GobDeepCopy() error {
105111
)
106112

107113
clone(&b1, &b2)
108-
output.Stdoutl("[b1, b2]", b1, b2, pa(&b1), pa(&b2))
114+
output.Stdoutl("[b1, b2][1]", b1, b2)
109115

110116
b1.Value = "world"
111117
b1.ValueB = "hello"
112-
output.Stdoutl("[b1, b2]", b1, b2, pa(&b1), pa(&b2))
118+
output.Stdoutl("[b1, b2][2]", b1, b2)
113119

114120
return nil
115121
}

0 commit comments

Comments
 (0)