go-channel.go 361 B

12345678910111213141516
  1. package common
  2. func SafeSend(ch chan bool, value bool) (closed bool) {
  3. defer func() {
  4. // Recover from panic if one occured. A panic would mean the channel was closed.
  5. if recover() != nil {
  6. closed = true
  7. }
  8. }()
  9. // This will panic if the channel is closed.
  10. ch <- value
  11. // If the code reaches here, then the channel was not closed.
  12. return false
  13. }