Level: AdvancedInterview

Go Channels Interview Questions: Select and Buffers (Part 15)

Updated 9 min read
Go Channels Interview Questions featured image: send, select and close cards

These Go channels interview questions cover unbuffered and buffered channels, close, range, the ok flag, select, nil channels and deadlocks. We gathered them from public interview lists, coding rounds and community threads. Then we rewrote them in plain words and grouped them by idea.

This post is questions only, so answer each one in your own words first. When you need help, read Go channels explained with examples and Go select statement. Also, finish Part 14 on goroutines first, because every channel needs goroutines on both ends.

How to Use These Go Channels Interview Questions

Answer each of these Go channels interview questions out loud, then write a tiny program to prove your answer. Channel bugs often end in a deadlock or a panic, so run every example. Finally, try the scenario questions, since they come from real production code.

The table below lists each section and its number of questions.

SectionWhat it testsQuestions
Channel basicsmake, send and receive15
Buffered channelsCapacity, len and bursts14
Close and rangeThe ok flag and closing panics15
The select statementdefault, timeouts and fairness15
Deadlocks and nil channelsBlocking, nil and directions15
Channel coding tasksClassic interview programs14
Go Channels Interview Questions diagram showing six question areas from channel basics to coding tasks
The six areas covered in this question list, from channel basics to coding tasks.

Go Channels Interview Questions on the Basics

Before you answer, study Go channels explained with examples. These Go channels interview questions check the core rules of sending and receiving.

  1. What is a channel in Go, and what problem does it solve?
  2. How do you create a channel, and what is its zero value?
  3. What does the arrow mean in ch <- v and in v := <-ch?
  4. When does a send on an unbuffered channel block?
  5. When does a receive block, and what wakes it up?
  6. Can many goroutines send on the same channel safely?
  7. What does the Go proverb about sharing memory by communicating mean?
  8. Which types can a channel carry, for example a struct or another channel?
  9. Why is a chan struct{} a common choice for signals?
  10. How do you pass a channel to a function, and does Go copy it?
  11. What are send-only and receive-only channel types?
  12. Why do function signatures often use directional channels?
  13. Can you compare two channels with ==, and when are they equal?
  14. Scenario: when a function must return results as they are ready, how would you design it with a channel?
  15. Scenario: when a goroutine must tell main that setup has finished, what kind of channel would you use?

Go Channels Interview Questions on Buffers

Before you answer, study Go worker pool pattern. Buffer size is a design choice, so these Go channels interview questions test your reasons, not only the syntax.

  1. What is a buffered channel, and how do you create one?
  2. When does a send on a buffered channel block?
  3. What do len and cap return for a channel?
  4. Why is checking len before a send not a safe way to avoid blocking?
  5. How do you choose a buffer size, and why is one a special case?
  6. When can a large buffer hide a real problem?
  7. How does a buffered channel work as a semaphore?
  8. Does a buffered channel keep the order of values?
  9. What is the difference in guarantees between buffered and unbuffered sends?
  10. Why can a buffered channel of size one prevent a goroutine leak?
  11. How does a buffered channel help with a fixed pool of tokens?
  12. Can you resize a channel after you create it?
  13. Scenario: when a producer is fast and a consumer is slow, what happens with a buffer of 1000?
  14. Scenario: when a team adds a buffer to fix a deadlock, how would you check if the fix is real?

Go Channels Interview Questions on Close and Range

Before you answer, study Go pipeline pattern. Closing rules cause many panics, so these Go channels interview questions come up in most concurrency rounds.

  1. What does close do to a channel?
  2. What does a receive from a closed channel return?
  3. How does the v, ok := <-ch form tell you that the sender closed the channel?
  4. What happens when you send on a closed channel?
  5. What happens when you close a channel twice, or close a nil channel?
  6. Who should close a channel, the sender or the receiver, and why?
  7. How does for range over a channel know when to stop?
  8. Do you always need to close a channel, for example before it gets garbage collected?
  9. How do you close a channel safely when many goroutines send on it?
  10. How does sync.Once help you close a channel only once?
  11. Why does closing a channel act as a broadcast to all receivers?
  12. Can you check if a channel is closed without receiving from it?
  13. How do you drain a channel before you shut down?
  14. Scenario: when a worker pool panics with send on closed channel during shutdown, how would you fix the order?
  15. Scenario: when a for range loop never ends, what would you check about the sender?

Go Channels Interview Questions on select

Before you answer, study Go select statement. The select statement joins channels with timeouts and cancellation, so it fills many Go channels interview questions.

  1. What does a select statement do?
  2. How does select choose when two cases are ready at the same time?
  3. What does a default case change in a select?
  4. How do you write a non-blocking send or receive?
  5. How do you add a timeout with time.After?
  6. Why should a long loop use a single timer instead of time.After on every pass?
  7. How do you stop a loop with a done channel inside select?
  8. How does ctx.Done work in a select case?
  9. What does an empty select{} do?
  10. Can you give one case priority over another, and how?
  11. What happens in a select when every channel is nil?
  12. How does a ticker work with select in a polling loop?
  13. Why can a busy loop with select and default burn CPU?
  14. Scenario: when a service must stop reading from a queue as soon as shutdown starts, how would you write the select?
  15. Scenario: when a call must give up after two seconds, how would you combine select with context?

Deadlocks, nil Channels and Directions

Before you answer, study Go rate limiting for a real channel design. These Go channels interview questions check how you avoid programs that stop forever.

  1. What does the error all goroutines are asleep mean?
  2. Why does a send on an unbuffered channel in main with no receiver deadlock?
  3. Can the runtime detect every deadlock, for example one inside a server?
  4. What does a nil channel do on send and on receive?
  5. How can setting a channel to nil turn off a case in select?
  6. What is a partial deadlock, and why is it hard to find?
  7. How do lock order and channel order both cause deadlocks?
  8. When should you use a mutex instead of a channel?
  9. How do you convert a bidirectional channel to a receive-only one?
  10. Can you close a receive-only channel, and what does the compiler say?
  11. How do you find a stuck goroutine with a goroutine dump?
  12. What does SIGQUIT print for a running Go program?
  13. How do channels help with backpressure in a pipeline?
  14. Scenario: when a program hangs with no error, how would you find which channel it waits on?
  15. Scenario: when merging two channels, one closes early, how would you keep reading from the other?

Go Channels Interview Questions for Coding Rounds

Before you answer, write each program by hand and run it with -race. Interviewers watch who closes the channel and how the program ends, not only the output.

  1. Print numbers 1 to 10 with two goroutines that take turns.
  2. Write a ping-pong program with two goroutines and one channel.
  3. Build a generator function that returns a receive-only channel.
  4. Merge two channels into one, and close it when both are done.
  5. Build a three stage pipeline that squares and then sums numbers.
  6. Write a fan-out, fan-in program with three workers.
  7. Limit concurrency with a buffered channel as a semaphore.
  8. Write a function that returns the first result from three mirrors.
  9. Write a timeout wrapper around a slow function with select.
  10. Build a simple pub-sub with one publisher and many subscribers.
  11. Implement a ticker that prints every 100 ms and stops after one second.
  12. Write a bounded queue with a buffered channel and non-blocking sends.
  13. Scenario: when an interviewer asks for a rate limiter of five requests per second, how would you build it with a ticker?
  14. Scenario: when a pipeline stage fails, how would you stop all stages without leaks?

Code Reading Questions

Read each program carefully and predict the output before you run it. Then explain the result in your own words.

package main

import "fmt"

func main() {
    ch := make(chan int, 3)
    ch <- 1
    ch <- 2
    fmt.Println(len(ch), cap(ch))
    close(ch)
    for v := range ch {
        fmt.Print(v, " ")
    }
    fmt.Println()
    v, ok := <-ch
    fmt.Println(v, ok)

    defer func() {
        fmt.Println("recovered:", recover())
    }()
    ch <- 3
}
  1. What do the first three print lines show, and why does range still read values after close?
  2. What message does the deferred function print, and why does the last send panic?

The second program uses nil channels to switch off cases and a timeout case.

package main

import (
    "fmt"
    "time"
)

func main() {
    a := make(chan string)
    b := make(chan string)
    go func() { a <- "from a"; close(a) }()
    go func() { b <- "from b"; close(b) }()

    got := 0
    for a != nil || b != nil {
        select {
        case v, ok := <-a:
            if !ok {
                a = nil
                continue
            }
            fmt.Println(v)
            got++
        case v, ok := <-b:
            if !ok {
                b = nil
                continue
            }
            fmt.Println(v)
            got++
        }
    }
    fmt.Println("messages:", got)

    idle := make(chan int)
    select {
    case n := <-idle:
        fmt.Println(n)
    case <-time.After(20 * time.Millisecond):
        fmt.Println("timeout")
    }
}
  1. Why can the first two lines appear in any order, and how does setting a channel to nil end the loop?
  2. What does the last select print, and why does it not block forever?

Quick Revision Table for Channel Rules

Use the table below as a final check before your interview. It lists what each operation does on each kind of channel.

OperationNil channelClosed channel
SendBlocks foreverPanics
ReceiveBlocks foreverZero value, ok is false
ClosePanicsPanics
len and capBoth zeroValues left and capacity
rangeBlocks foreverReads the rest, then stops

Also, many interviewers ask you to compare unbuffered and buffered channels directly. The diagram below shows the difference side by side.

Go channels diagram comparing an unbuffered channel with a buffered channel
An unbuffered send waits for a receiver, while a buffered send waits only when the buffer is full.

Official Resources

These official pages explain the rules behind these questions.

Quick Recap

  • These Go channels interview questions cover basics, buffers, close and range, select, deadlocks and coding tasks.
  • An unbuffered send waits for a receiver, while a buffered send waits only when full.
  • Only the sender closes a channel, and a send on a closed channel panics.
  • A receive from a closed channel returns the zero value with ok set to false.
  • Use select with ctx.Done or a timer, so a goroutine never waits forever.
  • A nil channel blocks forever, which is handy for turning off a select case.

What’s Next?

By now, you have Go channels interview questions for every part of channels and select. Study the answers in your own notes, then check them against the Go interview topics study guide.

Next up: Go sync package interview questions (Part 16)