goext/syncext/channel.go

15 lines
236 B
Go
Raw Normal View History

2022-11-30 23:34:16 +01:00
package dataext
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
}
}