How can I distribute functions for calling http requests in parallel?

package main import ( "fmt" "net/http" ) func foo() { for i := 0; i < 10; i++ { b, err := http.Get("http://golang.org") if err != nil { fmt.Println("Error") } else { fmt.Println(b.Header) fmt.Println(b.StatusCode) } } } func foo1() { for i := 0; i < 10; i++ { b, err := http.Get("http://golang.org") if err != nil { fmt.Println("Error") } else { fmt.Println(b.Header) fmt.Println(b.StatusCode) } } } func foo2() { for i := 0; i < 10; i++ { b, err := http.Get("http://golang.org") if err != nil { fmt.Println("Error") } else { fmt.Println(b.Header) fmt.Println(b.StatusCode) } } } func main() { foo(); foo1(); foo2(); fmt.Println("Ok") } 

Everything is done slowly and alternately. I tried through the gorutines go foo1 (); go foo2 () doesn't work either. What to do?

  • Run them in the gorutinakh: go function () - tilin
  • @tilin forgot to add * already tried and through go foo1 (); go foo2 (); doesn't work either - Mothership
  • You have 10 requests in each function that go in series, and they will not be parallel in any way. - tilin
  • @tilin uh ... and how to make multi-threaded? - Mothership

1 answer 1

 func foo() { b, err := http.Get("http://golang.org") if err != nil { fmt.Println("Error") } else { fmt.Println(b.Header) fmt.Println(b.StatusCode) } } func main() { for i := 0; i < 10; i++ { go foo() } var input string fmt.Scanln(&input) fmt.Println("Ok") } 
  • ABOUT! Now what the doctor ordered! Thank you very much - Mothership