Why doesn't this Golang code to select among multiple time.After channels work?
See code below. The 'timeout' message is never issued. Why?
package main
import (
"fmt"
"time"
)
func main() {
count := 0
for {
select {
case <-time.After(1 * time.Second):
count++
fmt.Printf("tick %d\n", count)
if count >= 5 {
fmt.Printf("ugh\n")
return
}
case <-time.After(3 * time.Second):
fmt.Printf("timeout\n")
return
}
}
}
Run it on Playground: http://play.golang.org/p/1gku-CWVAh
Output:
tick 1
tick 2
tick 3
tick 4
tick 5
ugh
Blocking Statements This means: A statement to receive data from a channel will block until some data is received. A statement to send data to a channel will wait until the sent data has been received.
In Go language, a channel is created using chan keyword and it can only transfer data of the same type, different types of data are not allowed to transport from the same channel. You can also create a channel using make() function using a shorthand declaration.
The select statement lets a goroutine wait on multiple communication operations. A select blocks until one of its cases can run, then it executes that case. It chooses one at random if multiple are ready.
Because time.After
is a function, so on every iteration it returns a new channel. If you want this channel to be the same for all iterations, you should save it before the loop:
timeout := time.After(3 * time.Second)
for {
select {
//...
case <-timeout:
fmt.Printf("timeout\n")
return
}
}
Playground: http://play.golang.org/p/muWLgTxpNf.
Even @Ainar-G has already provided the answer, another possibility is to use time.Tick(1e9)
to generate a time tick on every second and then listen for timeAfter
channel after the specified period.
package main
import (
"fmt"
"time"
)
func main() {
count := 0
timeTick := time.Tick(1 * time.Second)
timeAfter := time.After(5 * time.Second)
for {
select {
case <-timeTick:
count++
fmt.Printf("tick %d\n", count)
if count >= 5 {
fmt.Printf("ugh\n")
return
}
case <-timeAfter:
fmt.Printf("timeout\n")
return
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With