I have a channel:
aChan := make(chan struct{})
and a timeout duration var t time.Duration. I want the program to exit either if the channel is closed, or the t timeout is reached,
if t is a positive duration.
I know I can use an outer if else loop, but this looks very redundant:
if t >= time.Duration(0) {
select {
case <-time.After(t):
fmt.Fprintln(os.Stdout, "timeout!"))
close(timeoutChan)
case <-aChan:
fmt.Fprintln(os.Stdout, "aChan is closed"))
return
}
} else {
select {
case <-aChan:
fmt.Fprintln(os.Stdout, "aChan is closed"))
return
}
}
Is there a more elegant way to write this?