不然的话会deadlock
package main
import "fmt"
func squares(c chan int){
for i := 0;i<10;i++{
c <- i* i
}
close(c) // 注释掉这里的话就会deadlock
}
func main(){
fmt.Println("main() started")
c := make(chan int)
go squares(c)
for val := range c{
fmt.Println(val)
}
fmt.Println("main() stopped")
}
如果注释掉close语句的话,报错信息如下:
fatal error: all goroutines are asleep - deadlock!