Binary protocol gob and msgpack introduction

This paper describes the basic use of binary protocol gob and msgpack.

Recently I encountered a problem while writing session service a gin framework, Go language json package in numeric types (integer, floating point, etc.) serialization empty interface are stored in sequence into float64 type.

We construct a structure as follows:

type s struct {
    data map[string]interface{}
}

json serialization problem

func jsonDemo() {
    var s1 = s{
        data: make(map[string]interface{}, 8),
    }
    s1.data["count"] = 1
    ret, err := json.Marshal(s1.data)
    if err != nil {
        fmt.Println("marshal failed", err)
    }
    fmt.Printf("%#v\n", string(ret))
    var s2 = s{
        data: make(map[string]interface{}, 8),
    }
    err = json.Unmarshal(ret, &s2.data)
    if err != nil {
        fmt.Println("unmarshal failed", err)
    }
    fmt.Println(s2)
    for _, v := range s2.data {
        fmt.Printf("value:%v, type:%T\n", v, v)
    }
}

Output:

"{\"count\":1}"
{map[count:1]}
value:1, type:float64

Serialization exemplary gob

The standard library gob is "private" codec golang provided, its efficiency will be higher than json, xml, etc., especially for passing data between the Go language program.

func gobDemo() {
    var s1 = s{
        data: make(map[string]interface{}, 8),
    }
    s1.data["count"] = 1
    // encode
    buf := new(bytes.Buffer)
    enc := gob.NewEncoder(buf)
    err := enc.Encode(s1.data)
    if err != nil {
        fmt.Println("gob encode failed, err:", err)
        return
    }
    b := buf.Bytes()
    fmt.Println(b)
    var s2 = s{
        data: make(map[string]interface{}, 8),
    }
    // decode
    dec := gob.NewDecoder(bytes.NewBuffer(b))
    err = dec.Decode(&s2.data)
    if err != nil {
        fmt.Println("gob decode failed, err", err)
        return
    }
    fmt.Println(s2.data)
    for _, v := range s2.data {
        fmt.Printf("value:%v, type:%T\n", v, v)
    }
}

msgpack

MessagePack is an efficient binary serialization format. It allows you to exchange data between multiple languages (such as JSON). But it faster and smaller.

installation

go get -u github.com/vmihailenco/msgpack

Examples

package main

import (
    "fmt"

    "github.com/vmihailenco/msgpack"
)

// msgpack demo

type Person struct {
    Name   string
    Age    int
    Gender string
}

func main() {
    p1 := Person{
        Name:   "沙河娜扎",
        Age:    18,
        Gender: "男",
    }
    // marshal
    b, err := msgpack.Marshal(p1)
    if err != nil {
        fmt.Printf("msgpack marshal failed,err:%v", err)
        return
    }

    // unmarshal
    var p2 Person
    err = msgpack.Unmarshal(b, &p2)
    if err != nil {
        fmt.Printf("msgpack unmarshal failed,err:%v", err)
        return
    }
    fmt.Printf("p2:%#v\n", p2) // p2:main.Person{Name:"沙河娜扎", Age:18, Gender:"男"}
}

Guess you like

Origin www.cnblogs.com/nickchen121/p/11517433.html