// 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! (ya, that was me and Etel) package main import ( "fmt" "sync" ) var wg sync.WaitGroup var functionChan chan func (*sync.WaitGroup) type funcWait struct { f func() wgF sync.WaitGroup err error res int } func generateNumbers(total int, ch chan<- func (), wg *sync.WaitGroup) { defer wg.Done() fmt.Printf("sending %d to channel\n", total) ch <- func() { fmt.Printf("inside f() sending %d to channel\n", total) } } func andlabsGoroutine(ch <-chan func(), wg *sync.WaitGroup) { defer wg.Done() for f := range ch { fmt.Printf("read f() from channel\n") f() } } func main() { functionChan = make(chan func(*sync.WaitGroup)) r := make(chan func() (int, error)) res, err = (<-r)() wg.Add(1) go andlabsGoroutine(functionChan, &wg) for idx := 1; idx <= total; idx++ { generateNumbers(idx, functionChan, &wg) } close(functionChan) fmt.Println("Waiting for goroutines to finish...") wg.Wait() fmt.Println("Done!") }