Last Updated: February 25, 2016
· eefret

Always defer http.Response.Body.Close()

This is something that most of the new Golang developers always forgot, remember you must close the Response.Body after use, to avoid forgot this you can defer the Close and it will be executed at the finish of the function.


func someAwesomeFunc(param string) {
    //Lot of stuff
    resp, err := http.Get(URL.String())
    defer resp.Body.Close()
   //lot of stuff including resp

  //close will be executed here