From 1febb206eaaa70905c75562e173bb0a3ce4fcca7 Mon Sep 17 00:00:00 2001 From: Jian Han Date: Thu, 9 Nov 2017 10:53:20 +1000 Subject: [PATCH] added unbuffed example --- channel/unbuffed/main.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 channel/unbuffed/main.go diff --git a/channel/unbuffed/main.go b/channel/unbuffed/main.go new file mode 100644 index 0000000..d2a6b74 --- /dev/null +++ b/channel/unbuffed/main.go @@ -0,0 +1,33 @@ +package main + +import ( + "fmt" + "time" +) + +// https://nanxiao.gitbooks.io/golang-101-hacks/content/posts/unbuffered-and-buffered-channels.html +// After the main goroutine is launched, it will sleep immediately("Main goroutine sleeps 2 seconds" is printed), +// and this will cause main goroutine relinquishes the CPU to the func goroutine("Func goroutine begins sending data" is printed). +// But since the main goroutine is sleeping and can't receive data from the channel, +// so ch <- 1 operation in func goroutine can't complete until d := <- ch in main goroutine is executed(The final 3 logs are printed). +func main() { + + ch := make(chan int) + + go func(ch chan int) { + fmt.Println("Func goroutine begins sending data") + // sender will block on the channel until the receiver receives the data from the channel + ch <- 1 + fmt.Println("Func goroutine ends sending data") + }(ch) + + fmt.Println("Main goroutine sleeps 2 seconds") + time.Sleep(time.Second * 2) + + fmt.Println("Main goroutine begins receiving data") + // the receiver will also block on the channel until sender sends data into the channel. + d := <-ch + fmt.Println("Main goroutine received data:", d) + + time.Sleep(time.Second) +}