Answer the question
In order to leave comments, you need to log in
What's going on here?
for {
select {
case res := <-c1:
fmt.Println(res)
case <-time.After(time.Second * 10):
break
case <-time.After(time.Second * 1):
fmt.Println("timeout 1")
}
}
Answer the question
In order to leave comments, you need to log in
We don't go to the second branch because time.After creates a channel for us. Accordingly, at each iteration, the signal from time.After(1*time.Second) comes first, after which the next iteration of the loop occurs, and both time channels are recreated. Move time.After(10*time.Second) before the loop so that it doesn't recreate every iteration, and you'll be happy. like this:
package main
import (
"fmt"
"time"
)
func main() {
c1 := make(chan struct{}, 10)
time_limit := time.After(time.Second * 10)
Label1:
for {
select {
case res := <-c1:
fmt.Println(res)
case <-time_limit:
fmt.Println("time limit")
break Label1
case <-time.After(time.Second * 1):
fmt.Println("timeout 1")
}
}
}
package main
import (
"fmt"
"time"
)
func main() {
c1 := make(chan struct{}, 10)
time_limit := time.After(time.Second * 10)
func() {
for {
select {
case res := <-c1:
fmt.Println(res)
case <-time_limit:
fmt.Println("time_limit")
return
case <-time.After(time.Second * 1):
fmt.Println("timeout 1")
}
}
}() //просто анонимная функция
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question