dongsheng4679 2014-09-29 08:33
浏览 23
已采纳

Go解决方案,用于删除重复代码(延迟,net / http)

I have a below code in Go:

func (api *ApiResource) create(request *restful.Request, response *restful.Response) {
    account := &DefaultAccount
    err := request.ReadEntity(account)
    if err != nil {
        response.WriteErrorString(http.StatusInternalServerError, err.Error())
        return
    }
    tmpl := data_transformer.ParseTemplate("xml/accAdd.xml")
    payload := data_transformer.RenderTemplate(tmpl, account)
    resp, err := http.Post(url, "application/xml", payload)
    if err != nil {
        response.WriteErrorString(http.StatusInternalServerError, err.Error())
        return
    }

    // Body closes when either at the end of the function or at return
    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)

    aResp := new(AResp)
    err = xml.Unmarshal(body, aResp)
    if err != nil {
        fmt.Printf("error: %v", err)
        return
    }
    response.WriteHeader(http.StatusCreated)
    response.WriteEntity(aResp)
}

func (api *ApiResource) updateLimit(request *restful.Request, response *restful.Response) {
    account := &DefaultLimit
    err := request.ReadEntity(account)
    if err != nil {
        response.WriteErrorString(http.StatusInternalServerError, err.Error())
        return
    }
    tmpl := data_transformer.ParseTemplate("xml/addLimit.xml")
    payload := data_transformer.RenderTemplate(tmpl, account)
    resp, err := http.Post(url, "application/xml", payload)
    if err != nil {
        response.WriteErrorString(http.StatusInternalServerError, err.Error())
        return
    }

    // Body closes when either at the end of the function or at return
    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)

    aResp := new(AResp)
    err = xml.Unmarshal(body, aResp)
    if err != nil {
        fmt.Printf("error: %v", err)
        return
    }
    response.WriteHeader(http.StatusCreated)
    response.WriteEntity(aResp)
}

I want to be able to remove duplicate codes in an elegant way Go style.

If I do remove them and put them in separate function, would all defer and net/http package work as expected for different calls and xml file loads?

What is a good Go type solutions for this?

  • 写回答

1条回答 默认 最新

  • dtnpzghys01643322 2014-09-29 08:45
    关注

    You can separate almost the whole function and just pass to it the "account" and the xml file to read.

    Also it's a better practice to use xml.Decoder instead of xml.Unmarshal when reading from streams.

    func updateEntity(response *restful.Response, fn string, iface interface{}) {
        tmpl := data_transformer.ParseTemplate("xml/" + fn)
        payload := data_transformer.RenderTemplate(tmpl, iface)
        resp, err := http.Post(url, "application/xml", payload)
        if err != nil {
            response.WriteErrorString(http.StatusInternalServerError, err.Error())
            return
        }
    
        defer resp.Body.Close()
        dec := xml.NewDecoder(resp.Body)
        body, err := ioutil.ReadAll(resp.Body)
    
        aResp := &AResp{}
        err = dec.Decode(aResp)
        if err != nil {
            fmt.Printf("error: %v", err)
            return
        }
        response.WriteHeader(http.StatusCreated)
        response.WriteEntity(aResp)
    }
    
    func (*ApiResource) create(request *restful.Request, response *restful.Response) {
        account := &DefaultAccount
        err := request.ReadEntity(account)
        if err != nil {
            response.WriteErrorString(http.StatusInternalServerError, err.Error())
            return
        }
        updateEntity(response, "accAdd.xml", account)
    }
    func (*ApiResource) updateLimit(request *restful.Request, response *restful.Response) {
        account := &DefaultLimit
        err := request.ReadEntity(account)
        if err != nil {
            response.WriteErrorString(http.StatusInternalServerError, err.Error())
            return
        }
        updateEntity(response, "addLimit.xml", account)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 什么设备可以研究OFDM的60GHz毫米波信道模型
  • ¥15 不知道是该怎么引用多个函数片段
  • ¥15 爬取1-112页所有帖子的标题但是12页后要登录后才能 我使用selenium模拟登录 账号密码输入后 会报错 不知道怎么弄了
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题