github.com/qiuhoude/go-web@v0.0.0-20220223060959-ab545e78f20d/prepare/14_concurrent/channel/main.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "runtime" 6 "time" 7 ) 8 9 /* 10 A: 不能编译 11 B: 一段时间后总是输出 #goroutines: 1 12 C: 一段时间后总是输出 #goroutines: 2 答案 13 D: panic 14 */ 15 func main() { 16 var ch chan int 17 go func() { 18 ch = make(chan int, 1) 19 ch <- 1 20 fmt.Println("结束1") 21 }() 22 23 go func(ch chan int) { 24 time.Sleep(time.Second) 25 <-ch 26 fmt.Println("结束2") 27 }(ch) 28 29 c := time.Tick(1 * time.Second) 30 31 for range c { 32 fmt.Printf("#goroutines: %d\n", runtime.NumGoroutine()) 33 } 34 }