goroutine中的for range会阻塞
package main
import (
"fmt"
"time"
)
func main() {
var ch chan int // 未初始化,值为 nil
ch = make(chan int, 10)
go func() {
fmt.Println("start range")
for v := range ch {
fmt.Println("range ing")
fmt.Println(v)
}
}()
time.Sleep(20 * time.Second)
close(ch)
time.Sleep(2 * time.Second)
}
网友评论