// Chapter 39 example: select with default does not wait. If no channel is // ready, it runs default at once. // Run with: go run exercises/39/03-default.go package main import "fmt" // tryReceive takes a message if one is waiting, and does not wait otherwise. func tryReceive(messages chan string) { select { case msg := <-messages: fmt.Println("Got:", msg) default: fmt.Println("Nothing yet.") } } // trySend sends if there is room, and reports if there is not. func trySend(messages chan string, msg string) { select { case messages <- msg: fmt.Println("Sent:", msg) default: fmt.Println("Full, dropped:", msg) } } func main() { messages := make(chan string, 1) tryReceive(messages) trySend(messages, "hello") trySend(messages, "again") tryReceive(messages) tryReceive(messages) }