Sobes.tech
Junior — Middle

Kas Range-i ehitust saab kasutada kanalite loomisel programmeerimiskeeles?

sobes.tech AI

Vastus AI-lt

In Go, the range construct is often used to iterate over channels, allowing to receive values from the channel until it is closed.

Example of using range with a channel:

ch := make(chan int)
func() {
    for i := 0; i < 5; i++ {
        ch <- i
    }
    close(ch)
}()

for val := range ch {
    fmt.Println(val)
}

Here, range is used to read from the channel ch until it is closed. Thus, the range construct does not "create" channels, but is used for convenient reading from them.