首页 > 其他分享 >go binary.read invalid type *interface 的解决方法

go binary.read invalid type *interface 的解决方法

时间:2023-01-14 21:35:35浏览次数:50  
标签:binary return err read invalid dict interface model data

原来的代码

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{}
就好了

标签:binary,return,err,read,invalid,dict,interface,model,data
From: https://www.cnblogs.com/ifnk/p/17052579.html

相关文章