Last active
October 15, 2018 13:57
-
-
Save jreisinger/bf35d9c96fb818cf01f6d436c873c078 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" | |
| ) | |
| // emit words onto the channel until the timer kicks in | |
| func emit(wordChannel chan string) { | |
| defer close(wordChannel) | |
| words := []string{"The", "quick", "brown", "fox"} | |
| // timer | |
| t := time.NewTimer(time.Second * 2) | |
| i := 0 | |
| for { | |
| select { | |
| // someone is listening on the channel | |
| case wordChannel <- words[i]: | |
| i++ | |
| if i == len(words) { | |
| i = 0 | |
| } | |
| // timer goes off | |
| case <-t.C: | |
| return | |
| } | |
| } | |
| } | |
| func main() { | |
| wordCh := make(chan string) | |
| go emit(wordCh) | |
| 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