Question: /* The following program demonstrates a parallel loop in Go. However, this program should wait for the go routines to terminate before ending. Remove the
/* The following program demonstrates a parallel loop in Go. However, this program should wait for the go routines to terminate before ending. Remove the Sleep statement and replace it by a proper synchronization.*/
package main
import "fmt" import "time"
func main() { x := []int{3, 1, 4, 1, 5, 9, 2, 6} var y [8]int
// parallel loop for i, v := range x { go func (i int, v int) { y[i]= calcul(v) }(i,v) // call goroutine }
// add synchronization time.Sleep(1*time.Second) fmt.Println(y) }
func calcul(v int) (int) { return 2*v*v*v+v*v } }
/*Please explain reasons for code if possible, i've seen another solution which is different*/
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
