Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "errors"
- "fmt"
- "sync"
- "time"
- )
- func main() {
- done := make(chan bool)
- errChan := make(chan error)
- wg := new(sync.WaitGroup)
- for i := 0; i < 5; i++ {
- wg.Add(1)
- go Sl(wg, i, errChan)
- }
- go func(w *sync.WaitGroup, d chan bool) {
- w.Wait()
- d <- true
- }(wg, done)
- select {
- case <-done:
- case err := <-errChan:
- fmt.Println(err)
- }
- }
- func Sl(wg *sync.WaitGroup, i int, errChan chan error) {
- time.Sleep(5 * time.Second)
- fmt.Println("See: ", i)
- if i == 3 { // spits error then the process stops
- errChan <- errors.New("Some error")
- }
- wg.Done()
- }
Add Comment
Please, Sign In to add comment