mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 05:11:49 +08:00
Co-authored-by: zhouzilong.luke <zhouzilong.luke@bytedance.com> Co-authored-by: Joe Chen <jc@unknwon.io>
26 lines
382 B
Go
26 lines
382 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
var cnt = 0
|
|
|
|
func main() {
|
|
ch1 := make(chan int)
|
|
go pump(ch1) // pump hangs
|
|
fmt.Println(<-ch1) // prints only 0
|
|
|
|
time.Sleep(time.Second)
|
|
fmt.Println(cnt) // prints 1
|
|
|
|
}
|
|
|
|
func pump(ch chan int) {
|
|
for i := 0; ; i++ {
|
|
ch <- i // the channel will be block due to lack of consumer
|
|
cnt++ // this code will only execute once
|
|
}
|
|
}
|