Code with Go
Ask or search…
K
Links

Non-Blocking Channel Operations

This Go code demonstrates the use of the select statement with default cases to handle multiple channel operations. Let's go through it with inline comments:
package main
import "fmt"
func main() {
// Creating an unbuffered channel 'messages' for string communication
messages := make(chan string)
// Creating an unbuffered channel 'signals' for boolean communication
signals := make(chan bool)
// The first 'select' statement attempts to receive a message from 'messages'
select {
case msg := <-messages:
fmt.Println("received message", msg)
default:
fmt.Println("no message received")
}
// Sending the message "hi" to 'messages'
msg := "hi"
select {
case messages <- msg:
fmt.Println("sent message", msg)
default:
fmt.Println("no message sent")
}
// The third 'select' statement tries to receive a message from 'messages'
// or a signal from 'signals' with a 'default' case for handling no activity
select {
case msg := <-messages:
fmt.Println("received message", msg)
case sig := <-signals:
fmt.Println("received signal", sig)
default:
fmt.Println("no activity")
}
}
Explanation:
  1. 1.
    package main: Indicates that this Go file belongs to the main executable package.
  2. 2.
    import "fmt": Imports the "fmt" package for formatting and printing.
  3. 3.
    func main() { ... }: The main function, where the execution of the program begins.
  4. 4.
    messages := make(chan string): Creates an unbuffered channel named 'messages' for string communication.
  5. 5.
    signals := make(chan bool): Creates an unbuffered channel named 'signals' for boolean communication.
  6. 6.
    The first select statement attempts to receive a message from 'messages'. Since there is no message at this point, the default case is executed, and "no message received" is printed.
  7. 7.
    msg := "hi": Defines a string variable 'msg' with the value "hi".
  8. 8.
    The second select statement attempts to send the message "hi" to 'messages'. Since the channel is unbuffered and there is no receiver ready to receive the message, the default case is executed, and "no message sent" is printed.
  9. 9.
    The third select statement tries to receive a message from 'messages' or a signal from 'signals'. However, neither message nor signal is present, so the default case is executed, and "no activity" is printed.
In summary, this code demonstrates the use of the select statement with default cases to handle channel operations. It shows how to check for activity on channels, handle cases where a channel operation cannot proceed immediately, and provide default actions when no activity occurs.