title: go http requests in loop
body: <p>I've recently started with GoLang and tried following..</p>
  
  <pre><code>package main
  
  import (
      "fmt"
      "strings"
      "net/http"
      "io/ioutil"
  )
  
  func main() {
  
      url := "https://uri.api.dev"
  
      payload := strings.NewReader("param1=example&amp;version=2")
  
      req, _ := http.NewRequest("POST", url, payload)
  
      req.Header.Add("content-type", "application/x-www-form-urlencoded")
  
      for i := 1; i &lt;= 10; i++ {
  
          res, _ := http.DefaultClient.Do(req)
  
          body, _ := ioutil.ReadAll(res.Body)
  
          fmt.Println(string(body))
  
      }
  
      defer res.Body.Close()
  
  }
  </code></pre>
  
  <p>It throws 'undefined: res' error when i try to run this. I want to make 10 http requests to an API uri and <code>res.Body.Close()</code> at last to keep connection persistent for speed. How can we access res variable outside of its scope and make this code work.  Please help, Thanks</p>