main is the entry point for all Go programs.
()
| 32 | |
| 33 | // main is the entry point for all Go programs. |
| 34 | func main() { |
| 35 | // Create a work pool with 2 goroutines. |
| 36 | p := work.New(2) |
| 37 | |
| 38 | var wg sync.WaitGroup |
| 39 | wg.Add(100 * len(names)) |
| 40 | |
| 41 | for i := 0; i < 100; i++ { |
| 42 | // Iterate over the slice of names. |
| 43 | for _, name := range names { |
| 44 | // Create a namePrinter and provide the |
| 45 | // specific name. |
| 46 | np := namePrinter{ |
| 47 | name: name, |
| 48 | } |
| 49 | |
| 50 | go func() { |
| 51 | // Submit the task to be worked on. When RunTask |
| 52 | // returns we know it is being handled. |
| 53 | p.Run(&np) |
| 54 | wg.Done() |
| 55 | }() |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | wg.Wait() |
| 60 | |
| 61 | // Shutdown the work pool and wait for all existing work |
| 62 | // to be completed. |
| 63 | p.Shutdown() |
| 64 | } |