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 31 of 100

Explain how ’select’ is used with Go channels.?

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

In Go, the ‘select‘ statement is used for handling concurrent communication between different goroutines using channels. It allows a goroutine to wait on multiple channel operations and proceed with the one that is ready to proceed first. This is useful for managing timeouts, communicating among multiple goroutines, or handling the termination of goroutines.

The syntax of the ‘select‘ statement in Go is as follows:

select {
case <-ch1:
    // This block will execute when there is data ready to be read from ch1
case ch2 <- x:
    // This block will execute when there is data ready to be sent to ch2
case ch3, ok := <-ch3:
    // This block will execute when there is data ready to be read from ch3
    // The 'ok' variable will be set to 'false' if the channel is closed
default:
    // This block will execute if none of the channel operations above are ready
}

Here’s an example to demonstrate the usage of ‘select‘ with Go channels:

package main

import (
    "fmt"
    "time"
)

func main() {
    ch1 := make(chan string)
    ch2 := make(chan string)

    go func() {
        time.Sleep(1 * time.Second)
        ch1 <- "Goroutine 1"
    }()
    go func() {
        time.Sleep(2 * time.Second)
        ch2 <- "Goroutine 2"
    }()

    for i := 0; i < 2; i++ {
        select {
        case msg1 := <-ch1:
            fmt.Println("Received from ch1:", msg1)
        case msg2 := <-ch2:
            fmt.Println("Received from ch2:", msg2)
        }
    }
}

In this example, we have two goroutines that send their respective messages to channels ‘ch1‘ and ‘ch2‘ after certain durations. The ‘select‘ block inside the main function waits for messages from both the channels and prints them as soon as they are received.

Notice that the first goroutine waits for 1 second while the second goroutine waits for 2 seconds. This means that the first goroutine would end its operation before the second one does. However, because we are using ‘select‘, our main function does not block and wait for any specific goroutine. Instead, it proceeds with whichever channel operation is ready first, allowing us to effectively manage concurrency.

In this specific example, the output would look like:

Received from ch1: Goroutine 1
Received from ch2: Goroutine 2

The ‘select‘ statement is an important tool for handling concurrent communication between goroutines in Go. It allows your code to remain flexible and responsive to the completion of various channel operations without having to explicitly wait for each one to complete.

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