Sobes.tech
Junior — Middle

Is it possible to receive values from a single channel in multiple goroutines simultaneously?

sobes.tech AI

Answer from AI

Yes, multiple goroutines can simultaneously receive values from a single channel in Go. Channels in Go support multiple reading and writing, allowing several goroutines to read from the same channel in parallel.

However, it is important to note that if multiple goroutines are reading from the same channel, each value will be received by only one of them — values are not duplicated. This is useful for distributing work among goroutines (worker pool).

Example:

ch := make(chan int)

// Launch several goroutines that read from the channel
for i := 0; i < 3; i++ {
    go func(id int) {
        for val := range ch {
            fmt.Printf("Goroutine %d received %d\n", id, val)
        }
    }(i)
}

// Send data into the channel
for i := 0; i < 10; i++ {
    ch <- i
}
close(ch)

In this example, values from the channel are distributed among three goroutines.