Sobes.tech
Junior — Senior

Retrieving data from a channel with delay and time limit

livecode

Task condition

Determine which messages will appear in the program output, considering the channel operation, pauses between sends, and the timeout trigger in the select statement.

func main() {
  ch := make(chan int)
  go func() {
    for i := 0; i < 5; i++ {
      ch <- i
      time.Sleep(time.Second)
    }
    close(ch)
  }()

  for {
    select {
    case v := <-ch:
      fmt.Println(v)
    case <-time.After(3 * time.Second):
      fmt.Println("timeout")
      break
    }
  }
}