How to do it...

These steps cover writing and running your application:

  1. From your terminal/console application, create the chapter9/pool directory and navigate to it.
  2. Copy tests from https://github.com/agtorre/go-cookbook/tree/master/chapter9/pool or use this as an exercise to write some of your own code.
  3. Create a file called worker.go with the following content:
        package pool        import (            "context"            "fmt"        )        // Dispatch creates numWorker workers, returns a cancel         // function channels for adding work and responses,         // cancel must be called        func Dispatch(numWorker int) (context.CancelFunc, chan         WorkRequest, chan WorkResponse) {            ctx := context.Background()            ctx, cancel := context.WithCancel(ctx)            in := make(chan WorkRequest, 10) out := make(chan ...

Get Go Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.