msgpack 数据序列化/反序列化实例:
安装 msgpack:
1
go get gopkg.in/vmihailenco/msgpack.v2
实例
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
package main
import (
"fmt"
"gopkg.in/vmihailenco/msgpack.v2"
)
func main() {
ExampleMsgpack()
fmt.Printf("hello, world\n")
fmt.Println("hahha 22")
}
func ExampleMsgpack() {
type ts struct {
C string
K string
T int
Max int
Cn string
}
//var in = map[string]interface{}{"C": "LOCK", "K": "31uEbMgunupShBVTewXjtqbBv5MndwfXhb", "T": 1000, "Max": 200, "Cn": "中文"}
// or
var in = &ts{
C: "LOCK",
K: "31uEbMgunupShBVTewXjtqbBv5MndwfXhb",
T: 1000,
Max: 200,
Cn: "中文",
}
b, err := msgpack.Marshal(in)
if err != nil {
panic(err)
}
fmt.Printf("%#v\n", b)
var out = ts{}
err = msgpack.Unmarshal(b, &out)
if err != nil {
panic(err)
}
fmt.Println(out)
out.Max = 300
fmt.Println(out.Max)
}
注意: MessagePack: It's like JSON. but fast and small. http://msgpack.org/
本文网址: https://golangnote.com/topic/104.html 转摘请注明来源