package main import ( "fmt" "time" ) func main() { messages := make(chan string, 1) // Buffered channel // Non-blocking send select { case messages <- "hello": fmt.Println("Sent 'hello' immediately.") default: fmt.Println("No space in channel for 'hello'.") } // Try to send again, it should succeed now messages <- "world" fmt.Println("Sent 'world' after previous non-blocking attempt.") // Non-blocking receive select { case msg := <-messages: fmt.Println("Received immediately:", msg) default: fmt.Println("No message available immediately.") } // Channel is empty now, try non-blocking receive again select { case msg := <-messages: fmt.Println("Received immediately:", msg) default: fmt.Println("No message available immediately (second try).") } // Simulate some work, then send a message that will be picked up fmt.Println("Waiting 100ms then sending another message...") time.Sleep(100 * time.Millisecond) messages <- "delayed" // A blocking receive to ensure the delayed message is processed msg := <-messages fmt.Println("Received blocking:", msg) }