Skip to content

Instantly share code, notes, and snippets.

@badslug
Last active August 30, 2016 06:46
Show Gist options
  • Save badslug/22dba547e62b330a02cc05786596c5f6 to your computer and use it in GitHub Desktop.
Save badslug/22dba547e62b330a02cc05786596c5f6 to your computer and use it in GitHub Desktop.
Example showing a function that sends to a channel or times out
// 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