O'Reilly logo

Distributed Computing with Go by V.N. Nikhil Anurag

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

Solving the cashier problem with goroutines

Before we try to solve the problem, let's first formulate what we want to achieve:

  1. Create a channel orderChannel that accepts all orders.
  2. Launch the required number of cashier goroutines that accept limited numbers of orders from orderChannel.
  3. Start putting all orders into orderChannel.

Let's look at one possible solution that tries to solve the cashier problem using the preceding steps:

// wichan.go package main import ( "fmt" "sync" ) func cashier(cashierID int, orderChannel <-chan int, wg *sync.WaitGroup) { // Process orders upto limit. for ordersProcessed := 0; ordersProcessed < 10; ordersProcessed++ { // Retrieve order from orderChannel orderNum := <-orderChannel // Cashier is ready to serve! ...

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