Skip to content

Instantly share code, notes, and snippets.

@dink10
Forked from iamatypeofwalrus/channel_check.go
Created February 7, 2018 13:40
Show Gist options
  • Save dink10/701cf5e105364fd46a8d08e6331eea08 to your computer and use it in GitHub Desktop.
Save dink10/701cf5e105364fd46a8d08e6331eea08 to your computer and use it in GitHub Desktop.
Check if a Go Channel is open or closed
// An intersting pattern for testing, but you can use the check anywhere
import "testing"
func TestCheckingChannel(t *testing.T) {
stop := make(chan bool)
// Testing some fucntion that SHOULD close the channel
func (stop chan bool) {
close(chan)
}(stop)
// Make sure that the function does close the channel
_, ok := (<-stop)
// If we can recieve on the channel then it is NOT closed
if ok {
t.Error("Channel is not closed")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment