-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.go
More file actions
108 lines (92 loc) · 2.11 KB
/
main.go
File metadata and controls
108 lines (92 loc) · 2.11 KB
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
package main
import (
"encoding/json"
"fmt"
"github.com/fufuok/utils/orderedmap"
)
func main() {
// use New() instead of o := map[string]interface{}{}
o := orderedmap.New()
// use SetEscapeHTML() to whether escape problematic HTML characters or not, defaults is true
o.SetEscapeHTML(false)
// use Set instead of o["a"] = 1
o.Set("a", 1)
o.Set("c", 2)
// add some value with special characters
o.Set("b", "\\.<>[]{}_-")
// use Get instead of i, ok := o["a"]
val, ok := o.Get("a")
fmt.Println("Get:", val, ok)
// use Keys instead of for k, v := range o
keys := o.Keys()
for _, k := range keys {
v, _ := o.Get(k)
fmt.Println(k, v)
}
// use o.Delete instead of delete(o, key)
o.Delete("a")
// serialize to a json string using encoding/json
bytes, _ := json.Marshal(o)
fmt.Println("JSON:", string(bytes))
prettyBytes, _ := json.MarshalIndent(o, "", " ")
fmt.Println("JSON pretty:", string(prettyBytes))
// deserialize a json string using encoding/json
// all maps (including nested maps) will be parsed as orderedmaps
s := `{"a": 1, "c": 1.23, "d": 1.23, "b": -1, "e": 7}`
if err := json.Unmarshal([]byte(s), &o); err != nil {
fmt.Println(err)
return
}
fmt.Println("sort the keys:")
o.SortKeys()
for _, k := range o.Keys() {
v, _ := o.Get(k)
fmt.Println(k, v)
}
fmt.Println("sort by Pair:")
o.Sort(func(a *orderedmap.Pair, b *orderedmap.Pair) bool {
return a.Value().(float64) < b.Value().(float64)
})
for _, k := range o.Keys() {
v, _ := o.Get(k)
fmt.Println(k, v)
}
fmt.Println("sort by Pair(reverse):")
o.Sort(func(a *orderedmap.Pair, b *orderedmap.Pair) bool {
return a.Value().(float64) > b.Value().(float64)
})
for _, k := range o.Keys() {
v, _ := o.Get(k)
fmt.Println(k, v)
}
fmt.Println("size:", o.Size())
}
// Output:
// Get: 1 true
// a 1
// c 2
// b \.<>[]{}_-
// JSON: {"c":2,"b":"\\.\u003c\u003e[]{}_-"}
// JSON pretty: {
// "c": 2,
// "b": "\\.\u003c\u003e[]{}_-"
// }
// sort the keys:
// a 1
// b -1
// c 1.23
// d 1.23
// e 7
// sort by Pair:
// b -1
// a 1
// c 1.23
// d 1.23
// e 7
// sort by Pair(reverse):
// e 7
// c 1.23
// d 1.23
// a 1
// b -1
// size: 5