How to implement a concurrent worker pool using Goroutines and sync in Golang?

To implement a concurrent worker pool using Goroutines and sync in Golang, you can follow the steps below:

Step 1: Import the necessary packages

import ( "sync" )

Step 2: Define the worker function

func worker(id int, jobs <-chan int, results chan<- int) { for j := range jobs { // Do the work result := j * 2 // Send the result to the results channel results <- result } }

Step 3: Implement the worker pool

func main() { // Create the input and output channels jobs := make(chan int, 100) results := make(chan int, 100) // Create a WaitGroup to wait for all the workers to finish var wg sync.WaitGroup // Start the workers for i := 1; i <= 5; i++ { wg.Add(1) go func(workerID int) { // Decrease the WaitGroup counter when the worker finishes defer wg.Done() worker(workerID, jobs, results) }(i) } // Enqueue the jobs for i := 1; i <= 10; i++ { jobs <- i } close(jobs) // Wait for all the workers to finish wg.Wait() // Close the results channel after all the workers have finished close(results) // Process the results for result := range results { // Do something with the result fmt.Println(result) } }

In the above code, we create two channels, jobs and results, for communication between the main goroutine and the worker goroutines. We also create a WaitGroup to wait for all the workers to finish.

We then start the worker goroutines by providing them a worker function to execute. Each worker reads jobs from the jobs channel, performs some work, and sends the result to the results channel.

In the main goroutine, we enqueue the jobs onto the jobs channel, close it to indicate that no more jobs will be sent, and wait for all the workers to finish using the WaitGroup.

Finally, we close the results channel to indicate that no more results will be received, and process the results in the main goroutine as they arrive.

Note: You may need to import the necessary packages and modify the worker function to match your specific use case.