diff --git a/helper/README.md b/helper/README.md
index 7c502a9..dda2629 100644
--- a/helper/README.md
+++ b/helper/README.md
@@ -301,7 +301,7 @@ fmt.Println(helper.ChanToSlice(half)) // [1, 2, 3, 4]
func Drain[T any](c <-chan T)
```
-Drain drains the given channel.
+Drain drains the given channel. It blocks the caller.
## func [Duplicate]()
diff --git a/helper/buffered.go b/helper/buffered.go
index 623a2f3..ea91af6 100644
--- a/helper/buffered.go
+++ b/helper/buffered.go
@@ -12,9 +12,7 @@ package helper
func Buffered[T any](c <-chan T, size int) <-chan T {
result := make(chan T, size)
- go func() {
- Pipe(c, result)
- }()
+ go Pipe(c, result)
return result
}
diff --git a/helper/drain.go b/helper/drain.go
index 5d92754..20f70d6 100644
--- a/helper/drain.go
+++ b/helper/drain.go
@@ -4,14 +4,12 @@
package helper
-// Drain drains the given channel.
+// Drain drains the given channel. It blocks the caller.
func Drain[T any](c <-chan T) {
- go func() {
- for {
- _, ok := <-c
- if !ok {
- break
- }
+ for {
+ _, ok := <-c
+ if !ok {
+ break
}
- }()
+ }
}
diff --git a/helper/first.go b/helper/first.go
index 7798d8d..dfe9d1b 100644
--- a/helper/first.go
+++ b/helper/first.go
@@ -9,16 +9,17 @@ func First[T any](c <-chan T, count int) <-chan T {
result := make(chan T, cap(c))
go func() {
- defer close(result)
for i := 0; i < count; i++ {
n, ok := <-c
if !ok {
- return
+ break
}
result <- n
}
+ close(result)
+
Drain(c)
}()
diff --git a/helper/pipe.go b/helper/pipe.go
index cd940f5..9c8078d 100644
--- a/helper/pipe.go
+++ b/helper/pipe.go
@@ -14,10 +14,8 @@ package helper
// helper.Pipe(input, output)
// fmt.println(helper.ChanToSlice(output)) // [2, 4, 6, 8]
func Pipe[T any](f <-chan T, t chan<- T) {
- go func() {
- defer close(t)
- for n := range f {
- t <- n
- }
- }()
+ defer close(t)
+ for n := range f {
+ t <- n
+ }
}
diff --git a/helper/pipe_test.go b/helper/pipe_test.go
index 13a37ee..16ac2c3 100644
--- a/helper/pipe_test.go
+++ b/helper/pipe_test.go
@@ -16,7 +16,7 @@ func TestPipe(t *testing.T) {
input := helper.SliceToChan(expected)
output := make(chan int)
- helper.Pipe(input, output)
+ go helper.Pipe(input, output)
actual := helper.ChanToSlice(output)
if !reflect.DeepEqual(actual, expected) {