• 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关键字进行初始化
  • 相关阅读:
    Mysql主从复制(基于Log)
    Linux系统开机启动流程
    JS的 验证组织机构的合法性
    Linux以下基于TCP多线程聊天室(client)
    浅谈Java集合框架
    疯狂Java学习笔记(72)-----------大话程序猿面试
    Android自己定义View之组合控件 ---- LED数字时钟
    C/C++学习:函数指针
    springmvc+spring+jpa(hibernate)+redis+maven配置
    数组进行多少次OP操作,才干有序
  • 原文地址:https://www.cnblogs.com/EvildoerOne/p/8337208.html
Copyright © 2020-2023  润新知