Last active
October 31, 2025 13:49
-
-
Save jesselucas/179e70a684b6df18189fdaaa24f852cf to your computer and use it in GitHub Desktop.
Revisions
-
jesselucas revised this gist
Mar 9, 2017 . 1 changed file with 14 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,24 +7,33 @@ import ( ) func main() { // Create a wait group of any size wg := sync.WaitGroup{} waitCh := make(chan struct{}) wg.Add(10) // In another go routine Wait for the wait group to finish. go func() { // Run some actions for i := 0; i < 10; i++ { go func() { defer wg.Done() fmt.Println("do some action") // Uncomment to show timeout. // time.Sleep(100 * time.Millisecond) }() } wg.Wait() close(waitCh) }() // Block until the wait group is done or we timeout. select { case <-waitCh: fmt.Println("WaitGroup finished!") case <-time.After(100 * time.Millisecond): fmt.Println("WaitGroup timed out..") } } -
jesselucas renamed this gist
Jan 19, 2017 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
jesselucas created this gist
Jan 19, 2017 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,30 @@ package main import ( "fmt" "sync" "time" ) func main() { wg := sync.WaitGroup{} waitCh := make(chan struct{}) wg.Add(10) for i := 0; i < 10; i++ { wg.Done() } go func() { wg.Wait() close(waitCh) }() select { case <-waitCh: fmt.Println("WaitGroup finished!") case <-time.After(100 * time.Millisecond): fmt.Println("WaitGroup timed out..") } }