Skip to content

Instantly share code, notes, and snippets.

@jreisinger
Last active October 15, 2018 13:57
Show Gist options
  • Select an option

  • Save jreisinger/bf35d9c96fb818cf01f6d436c873c078 to your computer and use it in GitHub Desktop.

Select an option

Save jreisinger/bf35d9c96fb818cf01f6d436c873c078 to your computer and use it in GitHub Desktop.
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