Skip to content

Instantly share code, notes, and snippets.

@jreisinger
Created December 12, 2018 16:35
Show Gist options
  • Select an option

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

Select an option

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