Go解决方案,用于删除重复的代码(延迟,net/http) [英] Go solution for removing duplicate code (defer, net/http)

查看:37
本文介绍了Go解决方案,用于删除重复的代码(延迟,net/http)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Go中有以下代码:

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)
}

我希望能够以一种优雅的Go风格删除重复的代码.

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

如果我确实删除它们并将它们放在单独的函数中,那么对于不同的调用和xml文件加载,所有defer和net/http包都能按预期工作吗?

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?

什么是好的Go型解决方案?

What is a good Go type solutions for this?

推荐答案

您可以分离几乎整个函数,然后将要传递的帐户"和xml文件传递给它.

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

从流中读取时,最好使用 xml.Decoder 代替 xml.Unmarshal .

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)
}

这篇关于Go解决方案,用于删除重复的代码(延迟,net/http)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆