2,190
社区成员
发帖
与我相关
我的任务
分享
package main
import "fmt"
func sum(a []int, c chan int) {
total := 0
for _, v := range a {
total += v
}
c <- total // send total to c
}
func main() {
a := []int{7, 2, 8, -9, 4, 0}
c := make(chan int)
go sum(a[:len(a)/2], c)
go sum(a[len(a)/2:], c)
x, y := <-c, <-c // receive from c
fmt.Println(x, y, x + y)
}
d := make(chan int)
d <- 2
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [chan send]: