O'Reilly logo

Go Standard Library Cookbook by Radomir Sohlich

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

  1. Open the console and create the folder chapter10/recipe05.
  2. Navigate to the directory.
  3. Create the file syncgroup.go with the following content:
        package main        import "sync"        import "fmt"        func main() {          wg := &sync.WaitGroup{}          for i := 0; i < 10; i++ {            wg.Add(1)            go func(idx int) {              // Do some work              defer wg.Done()              fmt.Printf("Exiting %d\n", idx)            }(i)          }          wg.Wait()          fmt.Println("All done.")        }
  1. Execute the code by go run syncgroup.go.
  2. See the output:

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required