Golang New 关键字的小bug 未找到原因(暂时)

在使用new关键字对res 进行初始化不会出现SendXml方法中的xml.unmashal方法不成功现象, 而如果使用new关键字的分解动作对res先取零值结构体 然后取地址后,就会出现xml.unmashal不成功(無报错),初始估计是xml.unmashal对interface的地址操作出现误差,但是具体情况未知

func PostapiRequest(req interface{}) (res interface{}, response api.ExtensionResponse) {
body, err := xml.Marshal(req)
if err != nil {
log.Println("create byte xml err---->", err)
return
}

switch req.(type) {
case hotelSearchWithRooms.HotelSearchWithRoomsReq:
res = new(hotelSearchWithRooms.HotelSearchWithRoomsRes)
case hotelDetails.HotelDetailsReq:
res = new(hotelDetails.HotelDetailsRes)
case hotelCancellationPolicy.HotelCancelPolicyReq:
res = new(hotelCancellationPolicy.HotelCancelPolicyRes)
case countryList.CountryListReq:
res = new(countryList.CountryListRes)
case cityList.CityListReq:
res =new(cityList.CityListRes)
}

fmt.Println(string(body))
fmt.Println(reflect.TypeOf(req))
response.TimeNow = time.Now()

code, message, data := api.HttpClient.SendXml(api.APIurl, body, api.Heads, 0, res)

response.Code = code
response.Message = message
response.UrlLog = api.APIurl
response.BodyLog = utils.Bytes2String(body)
response.DataLog = utils.Bytes2String(data)
response.Err = err
if code != commobj.SUCCESS {
log.Println(message)
} else {
fmt.Printf("%+v\n", res)

}

return
}
暂时记录下来方便下次查找,目前建议尽量使用new关键字进行初始化

转载于:https://www.cnblogs.com/EvildoerOne/p/8337208.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值