Last active
August 30, 2016 06:46
-
-
Save badslug/22dba547e62b330a02cc05786596c5f6 to your computer and use it in GitHub Desktop.
Example showing a function that sends to a channel or times out
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Send an event to a channel or timeout. The channel can be buffered or not. | |
func Send(event []byte) { | |
select { | |
case channel <- event: | |
// Channel receives the event if there is room (buffered) or if a reader is ready on the channel (unbuffered) | |
case <- time.After(5 * time.Second): | |
// Timed out - event is not sent if you do nothing here | |
// Alternatively you can do something else with the event here | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment