Skip to content

Commit 497d549

Browse files
committed
Buffered channel
1 parent 6fd6f1b commit 497d549

File tree

1 file changed

+23
-0
lines changed
  • 02-channels/03-buffered-channel

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
func main() {
8+
ch := make(chan int, 6) // 6 is the number of values we are sending from goroutine. That's why buffer size = 6
9+
10+
go func() {
11+
defer close(ch)
12+
13+
// send all iterator values on channel without blocking
14+
for i := 0; i < 6; i++ {
15+
fmt.Printf("Sending: %d\n", i)
16+
ch <- i
17+
}
18+
}()
19+
20+
for v := range ch {
21+
fmt.Printf("Received: %v\n", v)
22+
}
23+
}

0 commit comments

Comments
 (0)