// https://www.digitalocean.com/community/tutorials/how-to-run-multiple-functions-concurrently-in-go // who came up with the idea of making community tutorials. that was a good idea! package main import ( "fmt" "sync" ) func generateNumbers(total int, ch chan<- int, wg *sync.WaitGroup) { defer wg.Done() for idx := 1; idx <= total; idx++ { fmt.Printf("START sending %d to channel\n", idx) ch <- idx } } func printInt(idx int, ch <-chan int, wg *sync.WaitGroup) { defer wg.Done() for num := range ch { fmt.Printf("%d: read %d from channel\n", idx, num) } } func main() { var wg sync.WaitGroup numberChan := make(chan int) for idx := 1; idx <= 5; idx++ { fmt.Printf("START printInt %d\n", idx) wg.Add(1) go printInt(idx, numberChan, &wg) } generateNumbers(20, numberChan, &wg) close(numberChan) fmt.Println("Waiting for goroutines to finish...") wg.Wait() fmt.Println("Done!") }