dongpao5658 2014-10-20 12:52
浏览 76
已采纳

如何使用作为接口参数传入的Struct操作解码的XML

Is there a way to manipulate decoded XML with struct passed in as interface parameter?

I'm passing struct as a parameter which has interface type but after decoding it with xml.Decode I cannot specify its fields to retrieve its fields as structs. I think go is complaining that it doesn't know which struct to look for unless it's specifically mentioned.

Below is an example function:

func UpdateEntity(response *restful.Response, xml_template string, dataStruct interface{}, respStruct interface{}) (*restful.Response, interface{}) {
    payload := renderCachedTemplate(response, xml_template, dataStruct)
    resp, err := http.Post(url, "application/xml", payload)
    if err != nil {
        response.WriteErrorString(http.StatusInternalServerError, err.Error())
        return nil, nil
    }

    defer resp.Body.Close()
    dec := xml.NewDecoder(resp.Body)

    if err = dec.Decode(respStruct); err != nil {
        fmt.Printf("error: %v", err)
        return nil, nil
    }
    return response, respStruct
}

func main() {
   resp, respStruct := UpdateEntity(response, "template.xml", LoginStruct{}, UserStruct{})
   fmt.Println(respStruct.Username)
}

This line fmt.Println(respStruct.Username) complains Username is not defined as interface{} has no field or method Username.

I'm passing UserStruct as its respStruct parameter which has Username field for example.

Is there a way I can use interface{} return variable and use pre-defined Struct fields?

  • 写回答

1条回答 默认 最新

  • duandang2123 2014-10-20 15:25
    关注

    Change the function to expect a pointer to a value instead of returning the value:

    func UpdateEntity(response *restful.Response, xml_template string, dataStruct interface{}, respStruct interface{}) *restful.Response {
        payload := renderCachedTemplate(response, xml_template, dataStruct)
        resp, err := http.Post(url, "application/xml", payload)
        if err != nil {
            response.WriteErrorString(http.StatusInternalServerError, err.Error())
            return nil
        }
    
        defer resp.Body.Close()
        dec := xml.NewDecoder(resp.Body)
    
        if err = dec.Decode(respStruct); err != nil {
            fmt.Printf("error: %v", err)
            return nil
        }
        return response
    }
    

    Pass a pointer to your value to the function and then use the updated value after the function call:

    func main() {
       var respStruct UserStruct
       resp := UpdateEntity(response, "template.xml", LoginStruct{}, &respStruct)
       fmt.Println(respStruct.Username)
    }
    

    Because the Decode method expects a pointer to a value, this change also fixes an error in the original code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器