Created
October 16, 2018 11:21
-
-
Save jreisinger/5c22ad0aada2ca1b83b5c6e55c23bd3e 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" | |
| "math/rand" | |
| "time" | |
| ) | |
| func reader(ch chan int) { | |
| t := time.NewTimer(10 * time.Second) | |
| for { | |
| select { | |
| case i := <-ch: | |
| fmt.Printf("%d\n", i) | |
| // disable ^ branch after 3 secs | |
| case <-t.C: | |
| ch = nil | |
| } | |
| } | |
| } | |
| func writer(ch chan int) { | |
| stopper := time.NewTimer(2 * time.Second) | |
| starter := time.NewTimer(5 * time.Second) | |
| savedCh := ch | |
| for { | |
| select { | |
| case ch <- rand.Intn(42): | |
| // turn off | |
| case <-stopper.C: | |
| ch = nil | |
| // turn on | |
| case <-starter.C: | |
| ch = savedCh | |
| } | |
| } | |
| } | |
| func main() { | |
| ch := make(chan int) | |
| go reader(ch) | |
| go writer(ch) | |
| time.Sleep(15 * time.Second) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment