Let’s start with a basic console program that connects to a few website urls and tests if the connection is successful. There are no goroutines at start, and all calls are made in order which is not very efficient.
funccheckUrls(urls []string) { c := make(chanstring) var wg sync.WaitGroup for _, link := range urls { wg.Add(1) go checkUrl(link, c, &wg) } gofunc() { wg.Wait() close(c) }() for msg := range c { fmt.Println(msg) } }
funccheckUrl(url string, c chanstring, wg *sync.WaitGroup) { defer (*wg).Done() _, err := http.Get(url) if err != nil { c <- "We could not reach: " + url } else { c <- "Success reaching the website: " + url } }