Sobes.tech
Senior

Kas sa kasutad, et lugeda kanalit töögoroutine'is?

sobes.tech AI

Vastus AI-lt

In worker goroutines for reading from the channel, the for range construct over the channel is commonly used. This allows sequentially receiving values from the channel until it is closed, which is convenient for processing tasks in workers.

Example:

func worker(ch <-chan Task) {
    for task := range ch {
        // process task
    }
}

This approach automatically terminates the loop when the channel is closed and does not require additional checks for data presence.