在使用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