<iframe style="border:1px solid" src="https://wide.b3log.org/playground/d4cc5ad44d62ec939d9769e6dbfadc7b.go" width="99%" height="600"></iframe>
为什么会出现死锁的问题?
fatal error: all goroutines are asleep - deadlock!
package main
import (
"fmt"
"math/rand"
)
func test(ch chan int) {
ch <- rand.Int() //向channel中写入一个64位的随机数
close(ch)
}
func main() {
chs := make([]chan int, 10) //直接创建10个的int类型通道的切片
for i := 0; i < 10; i++ {
chs[i] = make(chan int)
go test(chs[i])
}
for _, ch := range chs {
value := <-ch //阻塞等待退出信号
fmt.Println(value)
}
}
#5
更多评论
for i := 0; i < 10; i++ {
chs[i] = make(chan int)
go test(chs[i])
}
加上一句代码
#1