Solution Review: Sum With Goroutines
Understand the solution for the “Sum With Goroutines” challenge.
We'll cover the following
Solution overview
For this challenge, you were required to implement a multi-threaded application to sum the numbers in the provided lists. Let's start with the findParallelSum
function.
The findParallelSum
function needs to spawn goroutines equal to the number of lists provided. To receive data back from the goroutines, it creates a int
channel. Each goroutine is then provided the list it needs to sum and the channel it needs to write to.
The code for each goroutine is presented in sumGoRoutine
function. The function takes an int
slice and a channel as parameters. This function then calculates the sum of the slices provided by iterating through it and writes the local sum on a channel.
Finally, back in findParallelSum,
we receive the local sum from all the goroutines and cumulate it into a final sum we'll return.
Code
The complete code for this challenge is provided below.
Get hands-on with 1400+ tech skills courses.