Skip to content

Instantly share code, notes, and snippets.

@monkrus
Created December 26, 2022 20:08
Show Gist options
  • Save monkrus/6fc03c1039f9aec2fbb9f0f3aa334c3e to your computer and use it in GitHub Desktop.
Save monkrus/6fc03c1039f9aec2fbb9f0f3aa334c3e to your computer and use it in GitHub Desktop.
Select statement to check if the channel is empty or not
package main
import (
"fmt"
"time"
)
func main() {
ch := make(chan int)
// Writer (launched in own goroutine)
go func() {
time.Sleep(time.Duration(500) * time.Millisecond)
fmt.Println("Writing value 1515...")
// This send statement is a blocking operation for this thread
ch <- 1515
// Maybe the following won't have time to execute : never mind
fmt.Println("Value 1515 written.")
}()
// Reader: in main thread
time.Sleep(time.Duration(1000) * time.Millisecond)
fmt.Println("Reading first value (if exists)...")
// This receive operation is blocking,
// and there is a value ready for reading
select {
case x, ok := <-ch:
if ok {
fmt.Printf("Value %d was read.\n", x)
} else {
fmt.Println("Channel closed!")
}
default:
fmt.Println("No value ready, moving on.")
}
fmt.Println("Reading second value (if exists)...")
// This receive operation is blocking,
// and there is no value to read : BIG DEADLOCK HAZARD
select {
case x, ok := <-ch:
if ok {
fmt.Printf("Value %d was read.\n", x)
} else {
fmt.Println("Channel closed!")
}
default:
fmt.Println("No value ready, moving on.")
}
fmt.Println("The end")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment