nonblocking channel operations

This commit is contained in:
Mark McGranaghan 2012-09-30 20:32:44 -07:00
parent 1f30b700bd
commit 287decabfd
73 changed files with 36 additions and 1 deletions

View File

@ -0,0 +1,35 @@
package main
import "fmt"
func main() {
messages := make(chan string)
signals := make(chan bool)
// Non-blocking receive.
select {
case msg := <- messages:
fmt.Println("received message", msg)
default:
fmt.Println("no messages received")
}
// Non-blocking send.
msg := "hi"
select {
case messages <- msg:
fmt.Println("sent message", msg)
default:
fmt.Println("no messages sent")
}
// Non-blocking multi-way select.
select {
case msg := <- messages:
fmt.Println("received message", msg)
case sig := <- signals:
fmt.Println("received signal", sig)
default:
fmt.Println("no activity")
}
}

View File

@ -43,7 +43,7 @@ timeouts
scatter-gather
rate-limiting
worker-pools
nonblocking-channel-operations ~
nonblocking-channel-operations
closing-channels
timers
tickers