Last Updated: February 25, 2016
·
2.385K
· 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.

Ex:

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
}