原来的代码
binary.read invalid type *interface
func NewBusiness() *PacketBusiness {
var dict = make(map[uint32]interface{})
//dict[model.EventMCUUDPTBarPush] = model.TBarMcuData{}
dict[model.EventFPGAUDPSelfCheck] = model.FpgaState{}
return &PacketBusiness{dict: dict}
}
func (b *PacketBusiness) getFrameData(data []byte, code uint32) (interface{}, []model.DescItem) {
i, ok := b.dict[code]
if !ok {
return nil, make([]model.DescItem, 0)
}
log.Infof("data is %+v, code is %+v", data, code)
err := udp2.DecodeUserData(data, &i)
if err != nil {
runtime.LogInfo(b.ctx, fmt.Sprintf("decode mcu data error: %+v", err))
return i, make([]model.DescItem, 0)
}
descs := getStructInfo(&i)
return i, descs
}
func DecodeUserData(data []byte, object interface{}) error {
if err := binary.Read(bytes.NewBuffer(data), ByteOrder, object); err != nil {
return fmt.Errorf("DecodeUserData error: %s ", err.Error())
}
return nil
}
函数 DecodeUserData 会报错 ,标准库 binray.read 报 binary.read invalid type *interface
修改
把 dict[model.EventFPGAUDPSelfCheck] = model.FpgaState{} 初始化 改成 dict[model.EventFPGAUDPSelfCheck] = &model.FpgaState{}
就好了