go by example part 3
This commit is contained in:
42
5-go-by-example/30-non-blocking-channel-.go
Normal file
42
5-go-by-example/30-non-blocking-channel-.go
Normal file
@@ -0,0 +1,42 @@
|
||||
// Basic sends and receives on channels are blocking
|
||||
// use select with a default clause to implement non-blocking
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
messages := make(chan string)
|
||||
signals := make(chan bool)
|
||||
|
||||
// non-blocking receive
|
||||
// if a value is available on messages then select will take the <-messages case with that value
|
||||
// if not it will immediately take the default case
|
||||
select {
|
||||
case msg := <-messages:
|
||||
fmt.Println("received message", msg)
|
||||
default:
|
||||
fmt.Println("no message received")
|
||||
}
|
||||
|
||||
// non-blocking send
|
||||
// msg cannot be sent to the messages channel, because the channel has no buffer and there is no receiver
|
||||
// Therefore the default case is selected.
|
||||
msg := "hi"
|
||||
select {
|
||||
case messages <- msg:
|
||||
fmt.Println("sent message", msg)
|
||||
default:
|
||||
fmt.Println("no message sent")
|
||||
}
|
||||
|
||||
// use multiple cases above the default clause to implement a multi-way non-blocking select
|
||||
// here non-blocking receives on both messages and signals
|
||||
select {
|
||||
case msg := <-messages:
|
||||
fmt.Println("received message", msg)
|
||||
case sig := <-signals:
|
||||
fmt.Println("received signal", sig)
|
||||
default:
|
||||
fmt.Println("no activity")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user