33 lines
422 B
Go
33 lines
422 B
Go
|
package channel
|
||
|
|
||
|
func FindFirst[T any](source <-chan T) *T {
|
||
|
for v := range source {
|
||
|
return &v
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func FindFirstAndFlush[T any](source <-chan T) *T {
|
||
|
for v := range source {
|
||
|
Flush(source)
|
||
|
return &v
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func FindLast[T any](source <-chan T) *T {
|
||
|
var last *T = new(T)
|
||
|
found := false
|
||
|
|
||
|
for v := range source {
|
||
|
*last = v
|
||
|
found = true
|
||
|
}
|
||
|
|
||
|
if !found {
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
return last
|
||
|
}
|