Skip to content

Instantly share code, notes, and snippets.

@jesselucas
Last active October 31, 2025 13:49
Show Gist options
  • Select an option

  • Save jesselucas/179e70a684b6df18189fdaaa24f852cf to your computer and use it in GitHub Desktop.

Select an option

Save jesselucas/179e70a684b6df18189fdaaa24f852cf to your computer and use it in GitHub Desktop.

Revisions

  1. jesselucas revised this gist Mar 9, 2017. 1 changed file with 14 additions and 5 deletions.
    19 changes: 14 additions & 5 deletions changroup.go
    Original 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)
    for i := 0; i < 10; i++ {
    wg.Done()
    }

    // 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..")
    }

    }
  2. jesselucas renamed this gist Jan 19, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. jesselucas created this gist Jan 19, 2017.
    30 changes: 30 additions & 0 deletions gistfile1.txt
    Original 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..")
    }

    }