使用msgpack库可以很方便的做到。
代码语言:javascript复制package main
import (
"fmt"
"log"
"github.com/vmihailenco/msgpack/v5"
)
func main() {
tp := new(TStruct)
tp.M = map[string]string{
"hello": "hi",
}
tp.Data = []byte("hello world")
b, err := msgpack.Marshal(tp)
if err != nil {
panic(err)
}
// log.Printf("%s", b)
tp2 := new(TStruct)
err = msgpack.Unmarshal(b, tp2)
if err != nil {
panic(err)
}
log.Printf("%v", tp2)
}
type TStruct struct {
M map[string]string
Data []byte
}
func (t *TStruct) String() string {
return fmt.Sprintf("%v %s", t.M, t.Data)
}