-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexamples_test.go
67 lines (58 loc) · 1.08 KB
/
examples_test.go
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
package merger
import (
"encoding/json"
"fmt"
"log"
)
func ExampleMerge() {
// Create struct A
A := struct {
FieldA string
FieldB string
FieldC []int
}{
"aAaA",
"bBbB",
[]int{1, 2},
}
// Create struct B
B := struct {
FieldA string
FieldC []int
}{
"NewVal",
[]int{3, 4},
}
// Merge struct A and B together
V, err := Merge(A, B)
if err != nil {
log.Fatal(err)
}
// Serialize
ser, _ := json.Marshal(V)
fmt.Println(string(ser))
// Output: {"FieldA":"NewVal","FieldB":"bBbB","FieldC":[1,2,3,4]}
}
func ExampleMerge_nested() {
// Create two maps with nested types
A := map[string]interface{}{
"a": []int{1, 2},
"b": "aAaAaA",
"c": map[int]int{1: 10, 2: 20},
}
B := map[string]interface{}{
"a": []int{3, 4},
"b": "bBbBbB",
"c": map[int]int{1: 100, 3: 30},
"d": "Only in B",
}
// Merge map A and B together, including their values
V, err := Merge(A, B)
if err != nil {
log.Fatal(err)
}
// Serialize
ser, _ := json.Marshal(V)
fmt.Println(string(ser))
// Output: {"a":[1,2,3,4],"b":"bBbBbB","c":{"1":100,"2":20,"3":30},"d":"Only in B"}
}