goext/syncext/channel.go

15 lines
236 B
Go
Raw Normal View History

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