Go makes concurrency & parallelism easier, but you still need to learn a few new idioms. This talk shows how to write proper concurrent code in Go by showing you common mistakes.
_, order := range orders { outterLoop: for { for _, chef := range chefs { if !chef.isBusy() { chef.cookAndYell(order, wg) break outterLoop } } } } wg.Wait() } 3
_, order := range orders { outterLoop: for { for _, chef := range chefs { if !chef.isBusy() { chef.cookAndYell(order, wg) break outterLoop } } } } wg.Wait() } 3
for _, c := range chefs { go func(c *chef) { for o := range orderWheel { c.cook(o) } }(c) } for i, order := range orders { fmt.Printf("order %d: %s ", i, order.dish) orderWheel <- order } close(orderWheel) } 4
:= make(chan *order) for _, c := range chefs { wg.Add(1) go func(c *chef) { for o := range orderChan { c.cook(o) } wg.Done() }(c) } for i, order := range orders { fmt.Printf("order %d: %s ", i, order.dish) orderChan <- order } close(orderChan) wg.Wait() } 5