title: Close multiple goroutine if an error occurs in one in go
body: <p>consider this function :</p>
  <pre><code>func doAllWork() error {
  
      var wg sync.WaitGroup
  
      for i := 0; i &lt; 2; i++ {
  
          wg.add(1)
          go func() {
  
              defer wg.Done()
              for j := 0; j &lt; 10; j++ {
                  result, err := work(j)
                  if err != nil {
                      // can't use `return err` here
                      // what sould I put instead ? 
                      os.Exit(0)
                  }
              }
          }()
      }
      wg.Wait()
  
      return nil
  }
  </code></pre>
  <p>In each goroutine, the function <code>work()</code> is called 10 times. If one call to <code>work()</code> returns an error in any of the running goroutines, I want all the goroutines to stop immediately, and the program to exit.
  Is it ok to use <code>os.Exit()</code> here ? How should I handle this ?</p>
  <hr />
  <p><strong>Edit</strong>: this question is different from <a href="https://stackoverflow.com/questions/6807590/how-to-stop-a-goroutine">how to stop a goroutine</a> as here I need to close all goroutines if an error occurs in one</p>