I am trying to understand concurrency and parallelism. I need to calculate the maximum of an array using the go routine, channel wait group, and stored in a shared variable here is the below code getting deadlock, please give direction
package main
import (
"fmt"
"math"
)
func main() {
arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
max := math.MinInt32
ch := make(chan int)
for i := 0; i < len(arr); i++ {
go func(i int) {
ch <- arr[i]
}(i)
}
for i := 0; i < len(arr); i++ {
if max < <-ch {
max = <-ch
}
}
fmt.Println(max)
}