Unbuffered and buffered channels

The channel is divided into two categories: unbuffered and buffered.

(1) Unbuffered channel
For unbuffered channel, the sender will block on the channel until the receiver receives the data from the channel, whilst the receiver will also block on the channel until sender sends data into the channel. Check the following example:

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. func main() {
  7. ch := make(chan int)
  8. go func(ch chan int) {
  9. fmt.Println("Func goroutine begins sending data")
  10. ch <- 1
  11. fmt.Println("Func goroutine ends sending data")
  12. }(ch)
  13. fmt.Println("Main goroutine sleeps 2 seconds")
  14. time.Sleep(time.Second * 2)
  15. fmt.Println("Main goroutine begins receiving data")
  16. d := <- ch
  17. fmt.Println("Main goroutine received data:", d)
  18. time.Sleep(time.Second)
  19. }

The running result likes this:

  1. Main goroutine sleeps 2 seconds
  2. Func goroutine begins sending data
  3. Main goroutine begins receiving data
  4. Main goroutine received data: 1
  5. Func goroutine ends sending data

After the main goroutine is launched, it will sleep immediately(“Main goroutine sleeps 2 seconds“ is printed), and this will cause main goroutine relinquishes the CPU to the func goroutine(“Func goroutine begins sending data“ is printed). But since the main goroutine is sleeping and can’t receive data from the channel, so ch <- 1 operation in func goroutine can’t complete until d := <- ch in main goroutine is executed(The final 3 logs are printed).

(2) Buffered channel
Compared with unbuffered counterpart, the sender of buffered channel will block when there is no empty slot of the channel, while the receiver will block on the channel when it is empty. Modify the above example:

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. func main() {
  7. ch := make(chan int, 2)
  8. go func(ch chan int) {
  9. for i := 1; i <= 5; i++ {
  10. ch <- i
  11. fmt.Println("Func goroutine sends data: ", i)
  12. }
  13. close(ch)
  14. }(ch)
  15. fmt.Println("Main goroutine sleeps 2 seconds")
  16. time.Sleep(time.Second * 2)
  17. fmt.Println("Main goroutine begins receiving data")
  18. for d := range ch {
  19. fmt.Println("Main goroutine received data:", d)
  20. }
  21. }

The executing result is as follows:

  1. Main goroutine sleeps 2 seconds
  2. Func goroutine sends data: 1
  3. Func goroutine sends data: 2
  4. Main goroutine begins receiving data
  5. Main goroutine received data: 1
  6. Main goroutine received data: 2
  7. Main goroutine received data: 3
  8. Func goroutine sends data: 3
  9. Func goroutine sends data: 4
  10. Func goroutine sends data: 5
  11. Main goroutine received data: 4
  12. Main goroutine received data: 5

In this sample, since the channel has 2 slots, so the func goroutine will not block until it sends the third element.

P.S., “make(chan int, 0)“ is equal to “make(chan int)“, and it will create an unbuffered int channel too.