Skip to content

Instantly share code, notes, and snippets.

@jatin510
Created April 27, 2023 05:15
Show Gist options
  • Save jatin510/260bd77aead47fd95cd0ff80c68e8d7c to your computer and use it in GitHub Desktop.
Save jatin510/260bd77aead47fd95cd0ff80c68e8d7c to your computer and use it in GitHub Desktop.
Implementing wait group in golang
package main
import (
"log"
"net/http"
"sync"
"time"
)
func main() {
websites := []string{
"https://jatin510.dev/",
"https://github.com/",
"https://google.com/",
"https://linkedin.com/",
"https://jatin510.dev/",
"https://github.com/",
"https://google.com/",
"https://linkedin.com/",
}
start := time.Now()
wg := sync.WaitGroup{}
for _, website := range websites {
wg.Add(1)
go makeApiRequest(website, &wg)
}
wg.Wait()
log.Println("Total time: ", time.Since(start))
}
func makeApiRequest(website string, wg *sync.WaitGroup) {
defer wg.Done()
res, err := http.Get(website)
if err != nil {
log.Println("Error: ", err)
} else {
log.Println("Website", website, "has status code : ", res.StatusCode)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment