💻 Tech
Unbuffered channels are channels that don’t have size declared when created like this:
ch := make(chan int)
The problem with unbuffered channel is that the chan send blocks the execution until the value is received by another channel in another goroutine. The program below will not print “Sent” until the value is received by another channel. If the channel is buffered, for example with size 1, the send operation will not block the execution.
ch := make(chan int)
go func() {
ch <- 1 // blocks until received
fmt.Println("Sent") // not printed until after chan receive
}()
time.Sleep(2 * time.Second)
fmt.Println("Received", <-ch) // will unblock the send operation