Skip to content

Commit

Permalink
Buffered channel
Browse files Browse the repository at this point in the history
  • Loading branch information
aditya43 committed May 30, 2021
1 parent 6fd6f1b commit 497d549
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions 02-channels/03-buffered-channel/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"fmt"
)

func main() {
ch := make(chan int, 6) // 6 is the number of values we are sending from goroutine. That's why buffer size = 6

go func() {
defer close(ch)

// send all iterator values on channel without blocking
for i := 0; i < 6; i++ {
fmt.Printf("Sending: %d\n", i)
ch <- i
}
}()

for v := range ch {
fmt.Printf("Received: %v\n", v)
}
}

0 comments on commit 497d549

Please sign in to comment.