Skip to content

Instantly share code, notes, and snippets.

@jreisinger
Created October 16, 2018 11:21
Show Gist options
  • Select an option

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

Select an option

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