zoukankan      html  css  js  c++  java
  • 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 ", res)

    }

    return
    }
    暂时记录下来方便下次查找,目前建议尽量使用new关键字进行初始化
  • 相关阅读:
    八皇后(回溯经典)
    高精度阶乘(大数运算)
    跳棋(利用规范的数学方法)
    贪心砝码(分治法)
    大数乘方取余
    二分法查找
    汉诺塔(经典递归)(未完全明白)
    斐波那契数列和
    实验 7: OpenDaylight 实验——Python 中的 REST API 调用
    实验 6:OpenDaylight 实验——OpenDaylight 及 Postman 实现流表下发流表
  • 原文地址:https://www.cnblogs.com/EvildoerOne/p/8337208.html
Copyright © 2011-2022 走看看