Created
December 12, 2018 16:35
-
-
Save jreisinger/48731f264035564bde4d0c1e3f8ea6ba to your computer and use it in GitHub Desktop.
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
| package main | |
| import ( | |
| "fmt" | |
| "time" | |
| ) | |
| func emit(channelCh chan chan string, doneCh chan bool) { | |
| wordCh := make(chan string) | |
| defer close(wordCh) // close on return | |
| channelCh <- wordCh // send word channel down the channe channel :-) | |
| words := []string{"The", "quick", "brown", "fox"} | |
| timer := time.NewTimer(3 * time.Second) | |
| i := 0 | |
| for { | |
| select { | |
| case wordCh <- words[i]: // someone is receiving from the channel | |
| i++ | |
| if i == len(words) { | |
| i = 0 | |
| } | |
| case <-timer.C: // timer kicks in | |
| return | |
| } | |
| } | |
| } | |
| func main() { | |
| channelCh := make(chan chan string) | |
| doneCh := make(chan bool) | |
| go emit(channelCh, doneCh) | |
| // get the channel from the channel | |
| wordCh := <-channelCh | |
| for word := range wordCh { | |
| fmt.Printf("%s ", word) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment