Golang笔记

msgpack 数据序列化/反序列化实例

msgpack 数据序列化/反序列化实例:

安装 msgpack:

go get gopkg.in/vmihailenco/msgpack.v2

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/ 35

本文网址: https://golangnote.com/topic/104.html (转载注明出处)
关于GolangNote:记录在工作中使用golang 遇到、面临的相关问题及解决方法。如果你在这里获得一些知识或信息,解决你的编程问题,请考虑捐赠给不幸的人或者你喜欢的慈善机构,除捐赠外,种植树木、志愿服务或减少排碳的行为也很有益处。如果你有任何问题可以在下面 留言
Be the first to comment!
Captcha image
Relative Articles