I would do 
put it right after resp, err := client.Do(req)
if err!=nil{
    // wrap a an err return
    return errWrap(err)
}
defer resp.Body.Close() 
log.Printf("resp: %+v\n", resp)
if resp.StatusCode != http.StatusOK {
        }

Some linter may also suggest you catch the body.Close() err as well




On Saturday, January 27, 2018 at 10:49:01 PM UTC-8, evan wrote:
>
>
> in the code snippet below which is inside a function, is "defer 
> resp.Body.Close() " in the appropriate position?
> am i required to always do a  resp.Body.Close()?
> ...
>         resp, err := client.Do(req)
>
> log.Printf("resp: %+v\n", resp)
> if resp.StatusCode != http.StatusOK {
> return errors.NewHTTPError(resp.StatusCode)
> }
>         ....
> defer resp.Body.Close() 
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to