title: Correct use of fasthttp.Client combined with goroutines
body: <p>I am new to Go and am looking for the correct way of using <code>net/http</code> or <code>fasthttp</code> with goroutines. Unfortunately there are not many <code>fasthttp</code> client examples out there. </p>
  
  <p>I found the following code: (<strong>Example1</strong>)</p>
  
  <pre><code>package main
  
  import (
      "bufio"
      "fmt"
      "github.com/valyala/fasthttp"
      "log"
      "net"
      "os"
      "sync"
      "time"
  )
  
  func grabPage(fastClient *fasthttp.Client, i int, wg *sync.WaitGroup) {
      defer wg.Done()
      _, body, err := fastClient.GetTimeout(nil, "https://en.wikipedia.org/wiki/Immanuel_Kant", time.Duration(time.Second*20))
      if err != nil {
          log.Fatal(err)
      }
      f, err := os.Create(fmt.Sprintf("./data/%d.txt", i))
      if err != nil {
          log.Fatal(err)
      }
      defer f.Close()
      w := bufio.NewWriter(f)
      w.Write(body)
  }
  
  func main() {
      var wg sync.WaitGroup
      total := 500
  
      c := &amp;fasthttp.Client{
          Dial: func(addr string) (net.Conn, error) {
              return fasthttp.DialTimeout(addr, time.Second*10)
          },
          MaxConnsPerHost: total,
      }
  
      wg.Add(total)
      for index := 0; index &lt; total; index++ {
          go grabPage(c, index, &amp;wg)
      }
      wg.Wait()
  }
  </code></pre>
  
  <p>In this code the developer creates a <code>fasthttp.Client</code> instance in the <code>main()</code> function and passes it to the goroutine using <code>go grabPage(c, ...)</code>. For my understanding, this way you create one instance and all the requests use this one instance to do the job.</p>
  
  <p>On another page, a developer uses something like that: (<strong>Example2</strong>)</p>
  
  <pre><code>func grabPage(i int, wg *sync.WaitGroup) {
      defer wg.Done()
  
      fastClient := &amp;fasthttp.Client{
          Dial: func(addr string) (net.Conn, error) {
              return fasthttp.DialTimeout(addr, time.Second*10)
          },
          MaxConnsPerHost: 500,
      }
  
      _, body, err := fastClient.GetTimeout(nil, "https://en.wikipedia.org/wiki/Immanuel_Kant", time.Duration(time.Second*20))
      if err != nil {
          log.Fatal(err)
      }
      f, err := os.Create(fmt.Sprintf("./data/%d.txt", i))
      if err != nil {
          log.Fatal(err)
      }
      defer f.Close()
      w := bufio.NewWriter(f)
      w.Write(body)
  }
  </code></pre>
  
  <p>The big question is, are both solutions correct? 
  Or does the Example2 solution really create a new instance and use a lot of memory for every goroutine?</p>
  
  <p>I made the examples out of snippets for my question, in the Example2 for sure <code>defer</code> are missing. This is not part of the question.</p>
  
  <p>A small side question: 
  <code>(fastClient *fasthttp.Client, i int, wg *sync.WaitGroup)</code> -> fastClient and wg are pointers, so why call <code>grabPage(c, index, &amp;wg)</code> and not <code>grabPage(&amp;c, index, &amp;wg)</code>?</p>