Files
the-way-to-go_ZH_CN/eBook/examples/chapter_14/channel_block.go
Luke Zhou dc86a917a6 fix spelling error and some improper demo (#807)
Co-authored-by: zhouzilong.luke <zhouzilong.luke@bytedance.com>
Co-authored-by: Joe Chen <jc@unknwon.io>
2021-10-23 11:49:46 +08:00

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
}
}