This is a creation in Article, where the information may have evolved or changed.
The following summarizes the serialization of Go, the format commonly used in communication: Msgpack and JSON
Msgpack
Installation:
Go get go get github.com/vmihailenco/msgpack
Go install Github.com/vmihailenco/msgpack
Api:http://godoc.org/github.com/vmihailenco/msgpack
Func Exampleencode () {b, err: = Msgpack. Marshal (True) fmt. Printf ("%v% #v \ n", err, B)//Output: <nil> []byte{0xc3}}func Exampledecode () {var out bool err: = Msgpack . Unmarshal ([]byte{0xc3}, &out) fmt. Println (err, out)//Output: <nil> true}func examplemapstringinterface () {in: = map[string]interface{}{"foo": UInt32 (123456789), "Hello": "World"} B, err: = Msgpack. Marshal (in) _ = Err var out map[string]interface{} err = Msgpack. Unmarshal (b, &out) fmt. Printf ("%v% #v \ n", err, out)//Output: <nil> map[string]interface {} {"foo": 0xfecaefbe, "Hello": "World"}}func Exa Mplerecursivemapstringinterface () {buf: = &bytes. buffer{} ENC: = Msgpack. Newencoder (BUF) in: = map[string]interface{}{"foo": map[string]interface{}{"Hello": "World"} _ = Enc. Encode (in) Dec: = Msgpack. Newdecoder (BUF) Dec. Decodemapfunc = func (d *msgpack. Decoder) (interface{}, error) {n, err: = D.decodemaplen () if err! = Nil { return nil, err} m: = Make (map[string]interface{}, N) for I: = 0; I < n; i++ {mk, err: = D.decodestring () if err! = Nil {return nil, err} MV, Err: = D.decodeinterface () if err! = Nil {return nil, err} M[MK ] = mv} return m, nil} out, err: = Dec. Decodeinterface () fmt. Printf ("%v% #v \ n", err, out)//Output: <nil> map[string]interface {} {"Foo": map[string]interface {} {"Hello": "Worl D "}}}
In the network stream:
BUF: = &bytes. Buffer{}buf. Write ([]byte{164, 98, Max, BUF). Write ([]byte{164, 98, Max, max) Dec: = Msgpack. Newdecoder (BUF) for {out , err: = Dec. Decodebytes () if err! = nil {break } fmt. Printf ("%v% #v \ n", err, String (out))}
JSON
-->loads:
① parse the JSON string into the struct
-----------------JSON loads----------------//parse JSON string into struct package Mainimport ( "FMT" "Encoding/json") Func Main () { type carinfo struct { Id string License string Color int device string//"< device type > .< device id> " } type Carlist struct { Result int Message string Cars []carinfo } var msg carlist Json_str: = ' {' result ': 0, "message": "OK", "cars": [{"id": "311111", "License": "Yu A1111", "Color": 2, " Device ":" va3k.10001 "}, {" id ":" 311112 "," License ":" Yu A1112 "," Color ": 2," Device ":" Va3k.10002 "}]} ' err: = json. Unmarshal ([]byte (JSON_STR), &msg) if err! = Nil { FMT. PRINTLN ("JSON loads ERR:", err) } fmt. PRINTLN (msg)}# {0 ok [{yu 311111 A1111 2 va3k.10001} {311112 Yu A1112 2 va3k.10002}]}
* * The first letter of the struct is capitalized and corresponds to the JSON string
② parse the JSON string into interface
Func Main () {json_str: = ' {' result ': 0, ' message ': ' OK ', ' cars ': [{' id ': ' yu 311111 ', ' License ': ' A1111 ', ' Color ', ' 2 ', ' Device ': "Va3k.10001"}, {"id": "311112", "License": "Yu A1112", "Color": 2, "Device": "Va3k.10002"}]} ' var msg map[string]interface{ }err: = json. Unmarshal ([]byte (JSON_STR), &msg) If Err = = nil{fmt. PRINTLN (msg)}}# map[result:0 message:ok cars:[map[id: Yu 311111 license:a1111 color:2 device:va3k.10001] map[id:311112 License: Yu A1112 color:2 device:va3k.10002]]
-->dumps:
①
Direct
B: = []byte (' {' Name ': "Alice", "Body": "Hello", "Time": 1294706395881547000} ')
That's it, you can use Unmarshal to parse the
② using Marshal
Type Message struct { Name string Body string Time int64}m: = message{"Alice", "Hello", 1294706395881547000} B, err: = json. Marshal (M) #--and []byte (' {' Name ': "Alice", "Body": "Hello", "Time": 1294706395881547000} ')