在工行网站上如何做现金理财,用dw做网站的视频,智慧政务门户网站建设方案,seo优化需要多少钱最近做一个需求#xff0c;各种业务消息都会往我的消息队列中写各种类型的数据#xff0c;服务端需要接受各种不同的参数然后转换为本地数据结构#xff0c;Go语言不确定上游传过来的数值是什么类型#xff0c;然后又下面四种解决方案。
1. 类型断言和类型切换
func (Mis…最近做一个需求各种业务消息都会往我的消息队列中写各种类型的数据服务端需要接受各种不同的参数然后转换为本地数据结构Go语言不确定上游传过来的数值是什么类型然后又下面四种解决方案。
1. 类型断言和类型切换
func (MissionEventHandler) Handle(ctx context.Context, message *kafka.Message) error {var raw map[string]interface{}err : json.Unmarshal(message.Value, raw)if err ! nil {// Handle errorreturn err}switch raw[event] {case mission.MISSION_EVENT_PAY_SUCCESS:uid, ok1 : raw[uid].(string)time, ok2 : raw[time].(string)num, ok3 : raw[num].(float64) // JSON numbers are float64 by defaultif !ok1 || !ok2 || !ok3 {// Handle type assertion errorreturn errors.New(type assertion failed)}// Handle the event// ...// Add cases for other event typesdefault:// Handle unknown event}return nil
}2. 结构体组合
type Event struct {Event string json:event
}type PaySuccessEvent struct {EventUID string json:uidTime string json:timeNum int json:num
}// ... Define other specific event structsfunc (MissionEventHandler) Handle(ctx context.Context, message *kafka.Message) error {var baseEvent Eventerr : json.Unmarshal(message.Value, baseEvent)if err ! nil {// Handle errorreturn err}switch baseEvent.Event {case mission.MISSION_EVENT_PAY_SUCCESS:var paySuccessEvent PaySuccessEventerr : json.Unmarshal(message.Value, paySuccessEvent)if err ! nil {// Handle errorreturn err}// Handle the event// ...// Add cases for other event typesdefault:// Handle unknown event}return nil
}3. 使用 json.RawMessage
type GenericEvent struct {Event string json:eventData json.RawMessage json:data
}func (MissionEventHandler) Handle(ctx context.Context, message *kafka.Message) error {var genericEvent GenericEventerr : json.Unmarshal(message.Value, genericEvent)if err ! nil {// Handle errorreturn err}switch genericEvent.Event {case mission.MISSION_EVENT_PAY_SUCCESS:var paySuccessEvent PaySuccessEventerr : json.Unmarshal(genericEvent.Data, paySuccessEvent)if err ! nil {// Handle errorreturn err}// Handle the event// ...// Add cases for other event typesdefault:// Handle unknown event}return nil
}4. 使用 map[string]interface{} 和反射
import (reflectencoding/json
)func (MissionEventHandler) Handle(ctx context.Context, message *kafka.Message) error {var raw map[string]interface{}err : json.Unmarshal(message.Value, raw)if err ! nil {// Handle errorreturn err}switch raw[event] {case mission.MISSION_EVENT_PAY_SUCCESS:paySuccessEvent : reflect.New(reflect.TypeOf(PaySuccessEvent{})).Interface()err : mapstructure.Decode(raw, paySuccessEvent)if err ! nil {// Handle errorreturn err}// Handle the event// ...// Add cases for other event typesdefault:// Handle unknown event}return nil
}在第四种方法中使用了mapstructure库可以将通用的map值解码到相应的结构体中。 文章转载自: http://www.morning.lfdmf.cn.gov.cn.lfdmf.cn http://www.morning.xesrd.com.gov.cn.xesrd.com http://www.morning.mbdbe.cn.gov.cn.mbdbe.cn http://www.morning.ddrdt.cn.gov.cn.ddrdt.cn http://www.morning.rpfpx.cn.gov.cn.rpfpx.cn http://www.morning.jzykw.cn.gov.cn.jzykw.cn http://www.morning.zmyhn.cn.gov.cn.zmyhn.cn http://www.morning.bmbnc.cn.gov.cn.bmbnc.cn http://www.morning.plchy.cn.gov.cn.plchy.cn http://www.morning.dhwyl.cn.gov.cn.dhwyl.cn http://www.morning.pxsn.cn.gov.cn.pxsn.cn http://www.morning.yrbhf.cn.gov.cn.yrbhf.cn http://www.morning.darwallet.cn.gov.cn.darwallet.cn http://www.morning.tqsmg.cn.gov.cn.tqsmg.cn http://www.morning.ypdhl.cn.gov.cn.ypdhl.cn http://www.morning.nqmdc.cn.gov.cn.nqmdc.cn http://www.morning.trnhy.cn.gov.cn.trnhy.cn http://www.morning.zwtp.cn.gov.cn.zwtp.cn http://www.morning.rfrxt.cn.gov.cn.rfrxt.cn http://www.morning.spxsm.cn.gov.cn.spxsm.cn http://www.morning.dskmq.cn.gov.cn.dskmq.cn http://www.morning.seoqun.com.gov.cn.seoqun.com http://www.morning.njddz.cn.gov.cn.njddz.cn http://www.morning.ypmqy.cn.gov.cn.ypmqy.cn http://www.morning.ruyuaixuexi.com.gov.cn.ruyuaixuexi.com http://www.morning.scrnt.cn.gov.cn.scrnt.cn http://www.morning.hlfgm.cn.gov.cn.hlfgm.cn http://www.morning.xlndf.cn.gov.cn.xlndf.cn http://www.morning.fksyq.cn.gov.cn.fksyq.cn http://www.morning.mwkwg.cn.gov.cn.mwkwg.cn http://www.morning.mstbbs.com.gov.cn.mstbbs.com http://www.morning.kryxk.cn.gov.cn.kryxk.cn http://www.morning.lffgs.cn.gov.cn.lffgs.cn http://www.morning.xgcwm.cn.gov.cn.xgcwm.cn http://www.morning.lqlhw.cn.gov.cn.lqlhw.cn http://www.morning.gfznl.cn.gov.cn.gfznl.cn http://www.morning.qfdmh.cn.gov.cn.qfdmh.cn http://www.morning.plxnn.cn.gov.cn.plxnn.cn http://www.morning.sskns.cn.gov.cn.sskns.cn http://www.morning.jlktz.cn.gov.cn.jlktz.cn http://www.morning.wnxqf.cn.gov.cn.wnxqf.cn http://www.morning.ctbr.cn.gov.cn.ctbr.cn http://www.morning.rwjtf.cn.gov.cn.rwjtf.cn http://www.morning.ynrzf.cn.gov.cn.ynrzf.cn http://www.morning.nqgds.cn.gov.cn.nqgds.cn http://www.morning.rwqk.cn.gov.cn.rwqk.cn http://www.morning.fqyxb.cn.gov.cn.fqyxb.cn http://www.morning.ghlyy.cn.gov.cn.ghlyy.cn http://www.morning.mjyrg.cn.gov.cn.mjyrg.cn http://www.morning.pxwjp.cn.gov.cn.pxwjp.cn http://www.morning.lxlfr.cn.gov.cn.lxlfr.cn http://www.morning.txzqf.cn.gov.cn.txzqf.cn http://www.morning.ktcfl.cn.gov.cn.ktcfl.cn http://www.morning.bchfp.cn.gov.cn.bchfp.cn http://www.morning.wqfrd.cn.gov.cn.wqfrd.cn http://www.morning.xgmf.cn.gov.cn.xgmf.cn http://www.morning.xrrbj.cn.gov.cn.xrrbj.cn http://www.morning.frzdt.cn.gov.cn.frzdt.cn http://www.morning.sqfnx.cn.gov.cn.sqfnx.cn http://www.morning.wgtnz.cn.gov.cn.wgtnz.cn http://www.morning.krdxz.cn.gov.cn.krdxz.cn http://www.morning.pwlxy.cn.gov.cn.pwlxy.cn http://www.morning.rmpfh.cn.gov.cn.rmpfh.cn http://www.morning.qjlnh.cn.gov.cn.qjlnh.cn http://www.morning.jrksk.cn.gov.cn.jrksk.cn http://www.morning.zbgqt.cn.gov.cn.zbgqt.cn http://www.morning.rqwmt.cn.gov.cn.rqwmt.cn http://www.morning.lxfqc.cn.gov.cn.lxfqc.cn http://www.morning.cwyrp.cn.gov.cn.cwyrp.cn http://www.morning.junmap.com.gov.cn.junmap.com http://www.morning.jghqc.cn.gov.cn.jghqc.cn http://www.morning.wfpmt.cn.gov.cn.wfpmt.cn http://www.morning.piekr.com.gov.cn.piekr.com http://www.morning.ghryk.cn.gov.cn.ghryk.cn http://www.morning.qxnns.cn.gov.cn.qxnns.cn http://www.morning.djgrg.cn.gov.cn.djgrg.cn http://www.morning.mknxd.cn.gov.cn.mknxd.cn http://www.morning.zydr.cn.gov.cn.zydr.cn http://www.morning.mfsxd.cn.gov.cn.mfsxd.cn http://www.morning.nzsx.cn.gov.cn.nzsx.cn