Sobes.tech
Middle+

package main import "time" func worker() chan int { ch := make(chan int) go func() { time.Sleep(3 * time.Second) ch <- 42 }() return ch } func main() { timeStart := time.Now() _, _ = <-worker(), <-worker() println(int(time.Since(timeStart).Seconds())) // }

sobes.tech AI

Answer from AI

In this code, the worker function launches a goroutine that sends the number 42 to a channel after 3 seconds. In main, it reads from two such channels sequentially:

_, _ = <-worker(), <-worker()

This means the program first waits for the first channel to receive a value (3 seconds), then for the second (another 3 seconds), totaling about 6 seconds.

Output:

println(int(time.Since(timeStart).Seconds())) // 6

If the goal is to get both results in parallel and wait for them simultaneously, you should read from the channels concurrently, for example:

ch1 := worker()
ch2 := worker()
_, _ = <-ch1, <-ch2

In this case, the total time will be about 3 seconds, since both workers run simultaneously.

package main import "time" func worker() chan int {… - sobes.tech