This question already has an answer here:
I'm trying to undersand channels in Go. There is a simple code
package main
import (
"fmt"
"sync"
"time"
)
func main() {
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
i := 0
for {
select {
case <-time.After(3 * time.Second):
fmt.Println("EXIT: 3 seconds")
return
case <-time.After(1 * time.Second):
fmt.Println(i, "second")
}
i++
}
}()
wg.Wait()
}
I expect console result like this
0 second
1 second
2 second
EXIT: 3 seconds
But actually it is like this
0 second
1 second
2 second
3 second
4 second
5 second
6 second
...
What am I missing with this timer and how can I fix a problem?
</div>
It's because of for loop. Each circle of loop RightHandSide of case <-time.After(3 * time.Second):
in select
statement evaluated and give you fresh timer. For example this way things should work
stop := time.After(3 * time.Second)
for {
select {
case <-stop:
fmt.Println("EXIT: 3 seconds")
return
case <-time.After(1 * time.Second):
fmt.Println(i, "second")
}
}