I'm trying to understand what I'm doing wrong here...
done := make(chan bool)
stringsChan := make(chan string)
//starts a makeshift timeout on processing
go func() {
time.AfterFunc(time.Nanosecond*500, func() {
done <- true
})
}()
// consumer read from channel and prints value
go func() {
for {
select {
case s := <-stringsChan:
fmt.Println(s)
case <-done:
close(stringsChan)
return
}
}
}()
//producer, writes data to channel till done signal.
for i := range 200 {
select {
case <-done:
close(stringsChan)
break
default:
stringsChan <- fmt.Sprintf("%d", i)
}
}
The pattern i'm trying to recreate is having an operation being interrupted and have it gracefully end.
So I have a timer going and it writes to the done channel. I tried using two different done channels for the write and read operation, It feels like I should be writing the true flag twice since there's 2 reads but that also feels hacky.
