package main import ( "fmt" "time" ) func main() { messageCh := make(chan string, 1) // Buffered channel to allow a send without immediate receiver // Goroutine that might send a message after a delay go func() { time.Sleep(time.Second * 1) fmt.Println("Sender: Sending 'Hello!'") messageCh <- "Hello!" fmt.Println("Sender: Sent 'Hello!'") }() fmt.Println("Main: Attempting non-blocking read...") // Perform a non-blocking read select { case msg := <-messageCh: fmt.Printf("Main: Received message: '%s' ", msg) default: fmt.Println("Main: No message available, performing other work.") // Simulate other work time.Sleep(time.Millisecond * 500) fmt.Println("Main: Finished other work.") } fmt.Println("Main: First non-blocking read attempt complete.") // Wait a bit to ensure the message is sent if it wasn't there before time.Sleep(time.Second * 1) fmt.Println("Main: Attempting non-blocking read again (message might be there now)...") select { case msg := <-messageCh: fmt.Printf("Main: Received message: '%s' ", msg) default: fmt.Println("Main: Still no message available.") } fmt.Println("Main: Program finished.") }