Consider the following snippet of Go code:
c := make(chan string)
go func() {
time.Sleep(100 * time.Millisecond)
fmt.Println("Sending test1...")
c <- "test1"
fmt.Println("Sending test2...")
c <- "test2"
fmt.Println("Done sending")
}()
go func() {
for {
select {
case n := <-c:
fmt.Println("Received:", n)
}
}
}()
Full source: http://play.golang.org/p/yY6JQzDMvn
I expect that the first goroutine will block when attempting to write "test1"
to the channel until the receive in the second goroutine. The expected output would be:
Sending test1...
Received: test1
Sending test2...
Received: test2
However, when I actually run the example, I end up with:
Sending test1...
Sending test2...
Received: test1
Received: test2
It would appear by the output that the send is not blocking as one would expect. What's going on here?