golang 的MessagePack编解码包的试用

主要是对于golang messagepack 包的试用

项目代码

  • go.mod
module demoapp
go 1.15
require (
    github.com/davecgh/go-spew v1.1.1 // indirect
    github.com/kr/text v0.2.0 // indirect
    github.com/tinylib/msgp v1.1.5
    github.com/vmihailenco/msgpack/v5 v5.1.4
    gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
    gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 // indirect
)
  • 项目结构
├── Makefile
├── README.md
├── go.mod
├── go.sum
├── main.go
└── pkg
    ├── person.go
  • 代码说明
    Makefile 使用make 进行代码生成
 
gen:
    go generate ./pkg

go.mod

module demoapp
go 1.15
require (
    github.com/davecgh/go-spew v1.1.1 // indirect
    github.com/kr/text v0.2.0 // indirect
    github.com/tinylib/msgp v1.1.5
    github.com/vmihailenco/msgpack/v5 v5.1.4
    gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
    gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 // indirect
)

person.go 定义的person struct,同时集成了golang 的tag 能力方便进行序列化以及反序列话处理


//go:generate msgp
package pkg
// Person person for search
type Person struct {
    Name       string `msg:"name" msgpack:"name"`
    Address    string `msg:"address" msgpack:"address"`
    Age        int    `msg:"age" msgpack:"age"`
    Hidden     string `msg:"-" msgpack:"_"` // this field is ignored
    unexported bool   // this field is also ignored
}

main.go 集成了tinylib/msgp + vmihailenco/msgpack 进行互相转换处理

package main
import (
    "bytes"
    "demoapp/pkg"
    "log"
    "github.com/tinylib/msgp/msgp"
    "github.com/vmihailenco/msgpack/v5"
)
func main() {
    var buf bytes.Buffer
    myPerson := pkg.Person{
        Name:    "荣锋亮",
        Address: "beijing",
        Age:     33,
    }
    err := msgp.Encode(&buf, &myPerson)
    if err != nil {
        panic("encode some wrong" + err.Error())
    }
    var dstPerson pkg.Person
    var dstPerson2 *pkg.Person = &pkg.Person{}
    err = msgpack.Unmarshal(buf.Bytes(), &dstPerson)
    datas, err := dstPerson2.UnmarshalMsg(buf.Bytes())
    if err != nil {
        panic("uncode:" + err.Error())
    }
    if len(datas) > 0 {
        log.Panicf("%d bytes left over after UnmarshalMsg(): %q", len(datas), datas)
    }
    log.Println("from msgp: ", string(buf.Bytes()))
    log.Printf("msgpack:%v,msgp: %v", dstPerson, *dstPerson2)
    log.Println("from msgpack:", dstPerson.Name)
}

运行

  • 命令
make 
go run main.go
  • 效果

 

 

参考资料

https://github.com/tinylib/msgp
https://github.com/vmihailenco/msgpack
https://msgpack.uptrace.dev/
https://github.com/rongfengliang/messagepack-go-learning

posted on   荣锋亮  阅读(1691)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2020-01-04 0x 一个强大使用简单的nodejs 火焰图工具
2020-01-04 sjk 一个强大的jvm 分析工具
2019-01-04 lua-resty-qless-web UI 界面运行
2019-01-04 自定义pip 包开发简单说明
2019-01-04 ethr 微软开源的tcp udp http 网络性能测试工具
2019-01-04 openresty 集成lua-resty-mail +smtp2http 扩展灵活的mail 服务
2018-01-04 riotjs 简单使用&&browserify 构建

导航

< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
点击右上角即可分享
微信分享提示