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

Go · Basic · question 10 of 100

What is a channel in Go? How does it work?

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

A channel in Go is a powerful synchronization and communication primitive that allows goroutines (concurrent functions in Go) to send and receive values of a specific type. Channels effectively manage the communication and synchronization among goroutines, thereby avoiding race conditions and enabling a clear and safe way to share data. Channels are created using the ‘make()‘ built-in function, and their types are denoted as ‘chan T‘, where ‘T‘ is the type of the values transferred via the channel.

The key operations performed on channels are sending, receiving, and closing. Here’s a brief overview of how these operations work:

1. **Sending**: To send a value to a channel, you can use the channel’s send operator ‘<-‘. This operation blocks the sending goroutine until the value is received by another goroutine or until the channel’s buffer is available to hold the value.

Example:

   ch <- value

2. **Receiving**: To receive a value from a channel, you can also use the channel’s receive operator ‘<-‘, which blocks the receiving goroutine until a value is available. The received value can be assigned to a variable or used in an expression.

Example:

   value := <-ch

3. **Closing**: Channels can be closed using the ‘close()‘ built-in function, which indicates that no more values will be sent on the channel. Closed channels do not block on receiving, so it’s important to ensure that the receiving side properly checks for the closed state.

Example:

   close(ch)

Channels can be unbuffered or buffered, which affects how they behave during communication. Unbuffered channels have no buffer space and ensure synchronization by blocking the sender until the receiver receives the value. Buffered channels have a buffer of a specified capacity and only block when the buffer is full.

Let’s look at a simple example that demonstrates how channels are used in Go:

package main

import (
    "fmt"
    "time"
)

func printNumbers(ch chan int) {
    for i := 1; i <= 5; i++ {
        ch <- i // send i to the channel
    }
    close(ch) // close the channel
}

func main() {
    ch := make(chan int) // create an unbuffered channel

    go printNumbers(ch) // start a goroutine to send numbers to the channel

    for num := range ch { // receive values from the channel
        fmt.Println(num)
        time.Sleep(time.Second) // simulate some work
    }
}

This example has a ‘printNumbers‘ function that sends numbers from 1 to 5 to the provided channel. In the main function, we create an unbuffered channel and start a goroutine to call ‘printNumbers‘. Then, we loop through the channel and receive the values, printing them with a one-second delay between each value.

In conclusion, a channel in Go is a powerful mechanism for safe communication and synchronization between goroutines. Using channels allows your concurrent code to be more robust, efficient, and easier to reason about.

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