Skip to content

Commit

Permalink
Non blocking communication
Browse files Browse the repository at this point in the history
  • Loading branch information
aditya43 committed May 30, 2021
1 parent 65db96c commit f3e3a2a
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions 03-select/03-non-blocking-communication/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
"fmt"
"time"
)

func main() {
ch := make(chan string)

go func() {
for i := 1; i < 4; i++ {
time.Sleep(1 * time.Second)
ch <- fmt.Sprintf("Message %d", i)
}

}()

// if there is no value on channel, do not block.
for i := 0; i < 10; i++ {
select {
case m := <-ch:
fmt.Println(m)
default:
fmt.Println("No message received!")
}

// Do some processing..
fmt.Println("processing..")
time.Sleep(1500 * time.Millisecond)
}
}

0 comments on commit f3e3a2a

Please sign in to comment.