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)Semaphore信号量
    多线程编程(3)
    Oracle SQL*plus常用的命令和函数
    经典的开发工具
    Windows 7/Vista下通过组策略禁止USB接口
    浅谈.NET下的多线程
    SQLServer和Oracle常用函数对比
    注册表操作类
    利用using和try/finally语句来清理资源
  • 原文地址:https://www.cnblogs.com/EvildoerOne/p/8337208.html
Copyright © 2011-2022 走看看