Last active
December 22, 2016 11:48
-
-
Save Anwesh43/40370049e9f31823dde2ece7ee61c395 to your computer and use it in GitHub Desktop.
Code in go to demonstrate blocking vs non blocking channel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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