Skip to content

Instantly share code, notes, and snippets.

@Anwesh43
Last active December 22, 2016 11:48
Show Gist options
  • Save Anwesh43/40370049e9f31823dde2ece7ee61c395 to your computer and use it in GitHub Desktop.
Save Anwesh43/40370049e9f31823dde2ece7ee61c395 to your computer and use it in GitHub Desktop.
Code in go to demonstrate blocking vs non blocking channel
package main
import (
"fmt"
"time"
)
func otherRoutine(channel chan int, n int) {
for i := 0; i < n; i++ {
time.Sleep(time.Second)
channel <- i * 5
}
}
func main() {
channel := make(chan int)
n := 30
go otherRoutine(channel, n)
for i := 0; i < n; i++ {
select {
case r := <-channel:
fmt.Println(r)
default:
fmt.Println("test")
}
}
for i := 0; i < n; i++ {
r := <-channel
println(r)
println("test")
}
//time.Sleep(time.Second * 10)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment