How to Check If a Channel Is Closed in Go

Check if a Go channel is closed by using a select statement with a default case to detect immediate zero-value returns.

Use a select statement with a default case to check if a channel is closed without blocking. If the channel is closed, the receive operation returns the zero value immediately; if open, the default case executes.

func isClosed(ch <-chan struct{}) bool {
	select {
	case <-ch:
		return true
	default:
		return false
	}
}

Note: This only works reliably if you are sure no other goroutine is sending to the channel at that exact moment, or if the channel is unbuffered and you control the sends.