WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Go · Intermediate · question 34 of 100

Can you explain the difference between buffered and unbuffered channels in Go?

📕 Buy this interview preparation book: 100 Go questions & answers — PDF + EPUB for $5

Buffered and unbuffered channels are two types of channels in the Go programming language. Channels are a way to communicate between concurrent goroutines, allowing them to send and receive values of a specified type.

**Unbuffered Channels**

An unbuffered channel, also called a synchronous channel, has no buffer capacity. It means that the sender and receiver must be ready to exchange data at the same time. If one side is not ready, the other side will block until it becomes ready. This synchronous behavior provides a way to synchronize the activities of two or more goroutines.

Basic unbuffered channel creation:

ch := make(chan int)

Here is an example of unbuffered channel usage:

package main

import (
    "fmt"
)

func main() {
    ch := make(chan int)

    go func() {
        ch <- 42 // Send 42 to the channel; this will block until someone receives the value
    }()

    fmt.Println(<-ch) // Receive value from the channel and print it; this will unblock the sender
}

**Buffered Channels**

In contrast, a buffered channel, also known as an asynchronous channel, has a buffer capacity. The channel can hold a specific number of values before it starts blocking the sender, allowing some "slack" in the communication between sender and receiver.

Buffered channel creation with a specific capacity:

ch := make(chan int, capacity)

Here is an example of buffered channel usage:

package main

import (
    "fmt"
    "time"
)

func main() {
    ch := make(chan int, 2)

    go func() {
        for i := 1; i <= 4; i++ {
            ch <- i
            fmt.Printf("Sent %dn", i)
            time.Sleep(time.Second)
        }
        close(ch)
    }()

    for i := range ch {
        fmt.Printf("Received %dn", i)
        time.Sleep(2 * time.Second)
    }
}

In this example, the buffered channel allows the sender to send two values immediately without waiting for the receiver to process them. However, when the buffer is full, the sender will start blocking until the receiver reads a value from the channel.

**Summary**

Unbuffered channels are synchronous and block the sender/receiver until the other side is ready for communication. Buffered channels introduce some buffer capacity, allowing for asynchronous communication between sender and receiver up to a certain extent.

To represent the difference between buffered and unbuffered channels in a chart, you may consider the following example:

In this chart, the vertical bars indicate the moments when the unbuffered channel (in black) and the buffered channel (in blue) are blocked or ready. As you can see, the unbuffered channel blocks more frequently than the buffered channel because it requires both sender and receiver to be ready simultaneously.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Go interview — then scores it.
📞 Practice Go — free 15 min
📕 Buy this interview preparation book: 100 Go questions & answers — PDF + EPUB for $5

All 100 Go questions · All topics