【发布时间】:2020-09-20 23:01:47
【问题描述】:
我正在开发一个使用 msgpack 的 golang 项目。在代码中存在以下注释
// We use a fast path for hot structs.
在此之下,有一些结构体实现了自己的 marshall 方法,并且代码类似于以下内容
func (z *struct) MarshalMsg(b []byte) (o []byte, err error) {
o = msgp.Require(b, z.Msgsize())
// string "Field1"
o = append(o, 0x88, 0xa6, 0x54, 0x65, 0x61, 0x6d, 0x49, 0x64)
o = msgp.AppendString(o, z.Field1)
// string "Field2"
o = append(o, 0xa6, 0x55, 0x73, 0x65, 0x72, 0x49, 0x64)
o = msgp.AppendString(o, z.Field2)
// string "Field3"
o = append(o, 0xa5, 0x52, 0x6f, 0x6c, 0x65, 0x73)
o = msgp.AppendString(o, z.Field3)
return
}
我不确定这段代码究竟做了什么,+ hot structs 是什么意思我假设它是自定义编组?还有 append(o, 0xa5, 0x52, 0x6f, 0x6c, 0x65, 0x73) 行是做什么的?
【问题讨论】: