goext/syncext/channel.go
2022-11-30 23:35:47 +01:00

15 lines
236 B
Go

package syncext
import "time"
func ReadChannelWithTimeout[T any](c chan T, timeout time.Duration) (T, bool) {
afterCh := time.After(timeout)
select {
case rv := <-c:
return rv, true
case <-afterCh:
return *new(T), false
}
}